SlideShare a Scribd company logo
Improving 3rd Party Script Performance with
<IFRAME>s
Philip Tellis / philip@bluesmoon.info
Velocity 2013 / 2013-06-20
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 1
• Philip Tellis
• @bluesmoon
• philip@bluesmoon.info
• SOASTA
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 2
Do you use JavaScript?
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 3
<script src="..."></script>
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 4
<script src>
• Works well with browser lookahead
• But blocks everything
• Yes, you can use async or defer
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 5
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 6
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 7
document.createElement("script");
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 8
dynamic script node
1 Loads in parallel with the rest of the page
2 Still blocks the onload event
3 No telling when it will load up
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 9
The Method Queue Pattern
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 10
MQP
var _mq = _mq || [];
var s = document.createElement("script"),
t = document.getElementsByTagName("script")[0];
s.src="http://some.site.com/script.js";
t.parentNode.insertBefore(s, t);
// script.js will be available some time in the
// future, but we can call its methods
_mq.push(["method1", list, of, params]);
_mq.push(["method2", other, params]);
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 11
MQP
var self = this;
_mq = _mq || [];
while(_mq.length) {
// remove the first item from the queue
var params = _mq.shift();
// remove the method from the first item
var method = params.shift();
self[method].apply(self, params);
}
_mq.push = function(params) {
// remove the method from the first item
var method = params.shift();
self[method].apply(self, params);
}
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 12
That takes care of #3
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 13
But we still block onload
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 14
IFRAMEs to the rescue
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 15
But IFRAMEs block onload until the subpage has
loaded
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 16
(This sub-page intentionally left blank)
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 17
So here’s the code – Section I
// Section 1 - Create the iframe
var dom,doc,where,
iframe = document.createElement("iframe");
iframe.src = "javascript:false";
(iframe.frameElement || iframe).style.cssText =
"width: 0; height: 0; border: 0";
where = document.getElementsByTagName("script");
where = where[where.length - 1];
where.parentNode.insertBefore(iframe, where);
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 18
javascript:false is key to solving most
cross-domain issues
Ask me about about:blank
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 19
Except if the page developer sets
document.domain
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 20
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 21
The code – Section II
// Section 2 - handle document.domain
try {
doc = iframe.contentWindow.document;
}
catch(e) {
dom = document.domain;
iframe.src =
"javascript:var d=document.open();" +
"d.domain=’" + dom + "’;" +
"void(0);";
doc = iframe.contentWindow.document;
}
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 22
Only set document.domain if it has already
been set!
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 23
The code – Section III
// Section 3 - tell the iframe to load our script
doc.open()._l = function() {
var js = this.createElement("script");
if(dom)
this.domain = dom;
js.id = "js-iframe-async";
js.src = script_url;
this.body.appendChild(js);
};
doc.write(’<body onload="document._l();">’);
doc.close();
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 24
Notice that we’ve set document.domain again
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 25
Inside this function, document is the parent
document and this is the iframe!
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 26
Also, global variables inside _l() are global to the
parent window
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 27
Modify the MQP for IFRAME support
GLOBAL = window;
// Running in an iframe, and our script node’s
// id is js-iframe-async
if(window.parent != window &&
document.getElementById("js-iframe-async")) {
GLOBAL = window.parent;
}
GLOBAL._mq = GLOBAL._mq || [];
_mq = GLOBAL._mq;
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 28
GLOBAL refers to the parent window and window
refers to the iframe
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 29
So attach events to GLOBAL
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 30
Summary
• Create an iframe with src set to javascript:false
• Set document.domain if needed (twice)
• Write dynamic script node into iframe on iframe’s onload
event
• Alias parent window into iframe
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 31
Result: Happy Customers
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 32
Read all about it
• http://lognormal.com/blog/2012/12/12/the-script-loader-
pattern/
• https://www.w3.org/Bugs/Public/show_bug.cgi?id=21074
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 33
Cache busting with a far-future expires header
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 34
Some more code...
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 35
location.reload(true);
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 36
More completely
<script src="SCRIPT.js"></script>
<script>
var reqd_ver = location.search;
window.onload = function() {
var ver = SCRIPT.version;
if (ver < reqd_ver) {
location.reload(true);
}
};
</script>
The condition protects us from an infinite loop with bad proxies
and Firefox 3.5.11
Note: Don’t use location.hash – it messes with history on IE8.
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 37
And the blog post...
http://www.lognormal.com/blog/2012/06/17/more-
on-updating-boomerang/
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 38
Join us right after this guy...
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 39
Revolutionary RUM
1 Combine 1oz Cane Rum, 3/4oz Blood Orange Puree, &
1/2oz citrus infused syrup in a mixing glass
2 Add ice and shake vigourously
3 Strain into a chilled cocktail glass
4 Top with lime-mint foam
17:45, Evolution Café + Bar, Hyatt Lobby
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 40
• Philip Tellis
• @bluesmoon
• philip@bluesmoon.info
• www.SOASTA.com
• boomerang
• LogNormal Blog
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 41
Image Credits
• Stop Hammertime by Rich Anderson
http://www.flickr.com/photos/memestate/54408373/
• Nicholas Zakas by Ben Alman
http://www.flickr.com/photos/rj3/5635837522/
Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 42

More Related Content

Viewers also liked (18)

PPTX
Random Hacks of Kindness
opengovpartnership
 
PPTX
Learnings for Accessibility for iOS Platform
Tasneem Sayeed
 
PPTX
Dynamic and accessible web content with WAI-ARIA
Access iQ
 
PDF
Introduction to mobile accessibility - AccessU 2013
Henny Swan
 
PDF
The Yahoo Social Accessibility Lab
Nate Ebrahimoon
 
PDF
Ubiquitous Transactions - Financial Future and Accessibility
Ted Drake
 
PDF
Create Accessible Infographics
Ted Drake
 
PPTX
Designing for cognitive disabilities
Ruth Ellison
 
PDF
Android Accessibility - The missing manual
Ted Drake
 
PDF
Accessibility metrics Accessibility Data Metrics and Reporting – Industry Bes...
Ted Drake
 
PDF
Mystery Meat 2.0 – Making hidden mobile interactions accessible
Ted Drake
 
PDF
CSUN 2017 Success Criteria: Dependencies and Prioritization
Sean Kelly
 
PDF
The 7 minute accessibility assessment and app rating system
Aidan Tierney
 
PPTX
Mind your lang (for role=drinks at CSUN 2017)
Adrian Roselli
 
PDF
Rethinking Accessibility: Role-Based Analysis of WCAG 2.0 - CSUN 2017
Bill Tyler
 
PDF
Accessibility microinteractions: better user experience, happier developers
Aidan Tierney
 
PPTX
A Multidisciplinary Approach to Universal Design
Anders Skifte
 
POTX
Reusable acceptance criteria and test cases for accessibility
Intopia
 
Random Hacks of Kindness
opengovpartnership
 
Learnings for Accessibility for iOS Platform
Tasneem Sayeed
 
Dynamic and accessible web content with WAI-ARIA
Access iQ
 
Introduction to mobile accessibility - AccessU 2013
Henny Swan
 
The Yahoo Social Accessibility Lab
Nate Ebrahimoon
 
Ubiquitous Transactions - Financial Future and Accessibility
Ted Drake
 
Create Accessible Infographics
Ted Drake
 
Designing for cognitive disabilities
Ruth Ellison
 
Android Accessibility - The missing manual
Ted Drake
 
Accessibility metrics Accessibility Data Metrics and Reporting – Industry Bes...
Ted Drake
 
Mystery Meat 2.0 – Making hidden mobile interactions accessible
Ted Drake
 
CSUN 2017 Success Criteria: Dependencies and Prioritization
Sean Kelly
 
The 7 minute accessibility assessment and app rating system
Aidan Tierney
 
Mind your lang (for role=drinks at CSUN 2017)
Adrian Roselli
 
Rethinking Accessibility: Role-Based Analysis of WCAG 2.0 - CSUN 2017
Bill Tyler
 
Accessibility microinteractions: better user experience, happier developers
Aidan Tierney
 
A Multidisciplinary Approach to Universal Design
Anders Skifte
 
Reusable acceptance criteria and test cases for accessibility
Intopia
 

Similar to Improving 3rd Party Script Performance With IFrames (20)

PPTX
High Performance Social Plugins
Stoyan Stefanov
 
KEY
Modern iframe programming
benvinegar
 
PPT
Oscon 20080724
linkedin_resptest2
 
PPT
Sanjeev ghai 12
Praveen kumar
 
KEY
A rough guide to JavaScript Performance
allmarkedup
 
PPTX
JavaScript performance patterns
Stoyan Stefanov
 
PDF
Front-end optimisation & jQuery Internals
Artur Cistov
 
PPT
Web20expo 20080425
Media Gorod
 
PPT
Web 2.0 Expo: Even Faster Web Sites
Steve Souders
 
PDF
From nothing to a video under 2 seconds / Mikhail Sychev (YouTube)
Ontico
 
PDF
PrairieDevCon 2014 - Web Doesn't Mean Slow
dmethvin
 
PPT
Velocity EU 2012 - Third party scripts and you
Patrick Meenan
 
PDF
Front-end optimisation & jQuery Internals (Pycon)
Artur Cistov
 
PPTX
High Performance Snippets
Steve Souders
 
PPTX
YGLF 2015 - Boom Performance | Eran Zinman (daPulse)
Eran Zinman
 
PPTX
BOOM Performance
dapulse
 
PPTX
External JavaScript Widget Development Best Practices (updated) (v.1.1)
Volkan Özçelik
 
PDF
Developing High Performance Web Apps
Timothy Fisher
 
PPT
Building fast webapps, fast - Velocity 2010
marcuswestin
 
PPTX
Fronteers 20091105 (1)
guestbf04d7
 
High Performance Social Plugins
Stoyan Stefanov
 
Modern iframe programming
benvinegar
 
Oscon 20080724
linkedin_resptest2
 
Sanjeev ghai 12
Praveen kumar
 
A rough guide to JavaScript Performance
allmarkedup
 
JavaScript performance patterns
Stoyan Stefanov
 
Front-end optimisation & jQuery Internals
Artur Cistov
 
Web20expo 20080425
Media Gorod
 
Web 2.0 Expo: Even Faster Web Sites
Steve Souders
 
From nothing to a video under 2 seconds / Mikhail Sychev (YouTube)
Ontico
 
PrairieDevCon 2014 - Web Doesn't Mean Slow
dmethvin
 
Velocity EU 2012 - Third party scripts and you
Patrick Meenan
 
Front-end optimisation & jQuery Internals (Pycon)
Artur Cistov
 
High Performance Snippets
Steve Souders
 
YGLF 2015 - Boom Performance | Eran Zinman (daPulse)
Eran Zinman
 
BOOM Performance
dapulse
 
External JavaScript Widget Development Best Practices (updated) (v.1.1)
Volkan Özçelik
 
Developing High Performance Web Apps
Timothy Fisher
 
Building fast webapps, fast - Velocity 2010
marcuswestin
 
Fronteers 20091105 (1)
guestbf04d7
 
Ad

More from Philip Tellis (20)

PDF
Improving D3 Performance with CANVAS and other Hacks
Philip Tellis
 
PDF
Frontend Performance: Beginner to Expert to Crazy Person
Philip Tellis
 
PDF
Frontend Performance: De débutant à Expert à Fou Furieux
Philip Tellis
 
PDF
Frontend Performance: Expert to Crazy Person
Philip Tellis
 
PDF
Beyond Page Level Metrics
Philip Tellis
 
PDF
Frontend Performance: Beginner to Expert to Crazy Person (San Diego Web Perf ...
Philip Tellis
 
PDF
Frontend Performance: Beginner to Expert to Crazy Person
Philip Tellis
 
PDF
Frontend Performance: Beginner to Expert to Crazy Person
Philip Tellis
 
PDF
Frontend Performance: Beginner to Expert to Crazy Person
Philip Tellis
 
PDF
mmm... beacons
Philip Tellis
 
PDF
RUM Distillation 101 -- Part I
Philip Tellis
 
PDF
Extending Boomerang
Philip Tellis
 
PDF
Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"
Philip Tellis
 
PDF
The Statistics of Web Performance Analysis
Philip Tellis
 
PDF
Abusing JavaScript to Measure Web Performance
Philip Tellis
 
PDF
Rum for Breakfast
Philip Tellis
 
PDF
Analysing network characteristics with JavaScript
Philip Tellis
 
PDF
A Node.JS bag of goodies for analyzing Web Traffic
Philip Tellis
 
PDF
Input sanitization
Philip Tellis
 
PDF
Messing with JavaScript and the DOM to measure network characteristics
Philip Tellis
 
Improving D3 Performance with CANVAS and other Hacks
Philip Tellis
 
Frontend Performance: Beginner to Expert to Crazy Person
Philip Tellis
 
Frontend Performance: De débutant à Expert à Fou Furieux
Philip Tellis
 
Frontend Performance: Expert to Crazy Person
Philip Tellis
 
Beyond Page Level Metrics
Philip Tellis
 
Frontend Performance: Beginner to Expert to Crazy Person (San Diego Web Perf ...
Philip Tellis
 
Frontend Performance: Beginner to Expert to Crazy Person
Philip Tellis
 
Frontend Performance: Beginner to Expert to Crazy Person
Philip Tellis
 
Frontend Performance: Beginner to Expert to Crazy Person
Philip Tellis
 
mmm... beacons
Philip Tellis
 
RUM Distillation 101 -- Part I
Philip Tellis
 
Extending Boomerang
Philip Tellis
 
Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"
Philip Tellis
 
The Statistics of Web Performance Analysis
Philip Tellis
 
Abusing JavaScript to Measure Web Performance
Philip Tellis
 
Rum for Breakfast
Philip Tellis
 
Analysing network characteristics with JavaScript
Philip Tellis
 
A Node.JS bag of goodies for analyzing Web Traffic
Philip Tellis
 
Input sanitization
Philip Tellis
 
Messing with JavaScript and the DOM to measure network characteristics
Philip Tellis
 
Ad

Recently uploaded (20)

PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
PDF
Français Patch Tuesday - Juillet
Ivanti
 
PPTX
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
Français Patch Tuesday - Juillet
Ivanti
 
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 

Improving 3rd Party Script Performance With IFrames

  • 1. Improving 3rd Party Script Performance with <IFRAME>s Philip Tellis / [email protected] Velocity 2013 / 2013-06-20 Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 1
  • 2. • Philip Tellis • @bluesmoon • [email protected] • SOASTA Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 2
  • 3. Do you use JavaScript? Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 3
  • 4. <script src="..."></script> Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 4
  • 5. <script src> • Works well with browser lookahead • But blocks everything • Yes, you can use async or defer Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 5
  • 6. Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 6
  • 7. Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 7
  • 8. document.createElement("script"); Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 8
  • 9. dynamic script node 1 Loads in parallel with the rest of the page 2 Still blocks the onload event 3 No telling when it will load up Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 9
  • 10. The Method Queue Pattern Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 10
  • 11. MQP var _mq = _mq || []; var s = document.createElement("script"), t = document.getElementsByTagName("script")[0]; s.src="http://some.site.com/script.js"; t.parentNode.insertBefore(s, t); // script.js will be available some time in the // future, but we can call its methods _mq.push(["method1", list, of, params]); _mq.push(["method2", other, params]); Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 11
  • 12. MQP var self = this; _mq = _mq || []; while(_mq.length) { // remove the first item from the queue var params = _mq.shift(); // remove the method from the first item var method = params.shift(); self[method].apply(self, params); } _mq.push = function(params) { // remove the method from the first item var method = params.shift(); self[method].apply(self, params); } Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 12
  • 13. That takes care of #3 Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 13
  • 14. But we still block onload Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 14
  • 15. IFRAMEs to the rescue Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 15
  • 16. But IFRAMEs block onload until the subpage has loaded Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 16
  • 17. (This sub-page intentionally left blank) Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 17
  • 18. So here’s the code – Section I // Section 1 - Create the iframe var dom,doc,where, iframe = document.createElement("iframe"); iframe.src = "javascript:false"; (iframe.frameElement || iframe).style.cssText = "width: 0; height: 0; border: 0"; where = document.getElementsByTagName("script"); where = where[where.length - 1]; where.parentNode.insertBefore(iframe, where); Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 18
  • 19. javascript:false is key to solving most cross-domain issues Ask me about about:blank Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 19
  • 20. Except if the page developer sets document.domain Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 20
  • 21. Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 21
  • 22. The code – Section II // Section 2 - handle document.domain try { doc = iframe.contentWindow.document; } catch(e) { dom = document.domain; iframe.src = "javascript:var d=document.open();" + "d.domain=’" + dom + "’;" + "void(0);"; doc = iframe.contentWindow.document; } Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 22
  • 23. Only set document.domain if it has already been set! Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 23
  • 24. The code – Section III // Section 3 - tell the iframe to load our script doc.open()._l = function() { var js = this.createElement("script"); if(dom) this.domain = dom; js.id = "js-iframe-async"; js.src = script_url; this.body.appendChild(js); }; doc.write(’<body onload="document._l();">’); doc.close(); Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 24
  • 25. Notice that we’ve set document.domain again Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 25
  • 26. Inside this function, document is the parent document and this is the iframe! Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 26
  • 27. Also, global variables inside _l() are global to the parent window Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 27
  • 28. Modify the MQP for IFRAME support GLOBAL = window; // Running in an iframe, and our script node’s // id is js-iframe-async if(window.parent != window && document.getElementById("js-iframe-async")) { GLOBAL = window.parent; } GLOBAL._mq = GLOBAL._mq || []; _mq = GLOBAL._mq; Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 28
  • 29. GLOBAL refers to the parent window and window refers to the iframe Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 29
  • 30. So attach events to GLOBAL Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 30
  • 31. Summary • Create an iframe with src set to javascript:false • Set document.domain if needed (twice) • Write dynamic script node into iframe on iframe’s onload event • Alias parent window into iframe Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 31
  • 32. Result: Happy Customers Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 32
  • 33. Read all about it • http://lognormal.com/blog/2012/12/12/the-script-loader- pattern/ • https://www.w3.org/Bugs/Public/show_bug.cgi?id=21074 Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 33
  • 34. Cache busting with a far-future expires header Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 34
  • 35. Some more code... Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 35
  • 36. location.reload(true); Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 36
  • 37. More completely <script src="SCRIPT.js"></script> <script> var reqd_ver = location.search; window.onload = function() { var ver = SCRIPT.version; if (ver < reqd_ver) { location.reload(true); } }; </script> The condition protects us from an infinite loop with bad proxies and Firefox 3.5.11 Note: Don’t use location.hash – it messes with history on IE8. Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 37
  • 38. And the blog post... http://www.lognormal.com/blog/2012/06/17/more- on-updating-boomerang/ Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 38
  • 39. Join us right after this guy... Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 39
  • 40. Revolutionary RUM 1 Combine 1oz Cane Rum, 3/4oz Blood Orange Puree, & 1/2oz citrus infused syrup in a mixing glass 2 Add ice and shake vigourously 3 Strain into a chilled cocktail glass 4 Top with lime-mint foam 17:45, Evolution Café + Bar, Hyatt Lobby Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 40
  • 41. • Philip Tellis • @bluesmoon • [email protected] • www.SOASTA.com • boomerang • LogNormal Blog Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 41
  • 42. Image Credits • Stop Hammertime by Rich Anderson http://www.flickr.com/photos/memestate/54408373/ • Nicholas Zakas by Ben Alman http://www.flickr.com/photos/rj3/5635837522/ Velocity 2013 / 2013-06-20 Improving 3rd Party Script Performance with <IFRAME>s 42