Skip to content

Commit

Permalink
Load scripts synchronously
Browse files Browse the repository at this point in the history
External scripts were being asynchronously loaded as part of the new
page being loaded. This could break inlined scripts that depended on
such scripts.

See original dieulot/instantclick#85 for
details
  • Loading branch information
Dmitry Lakhno committed Aug 25, 2014
1 parent b33d004 commit 9a4fc2e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "trueinstantclick",
"description": "TrueInstantClick makes following links in your website instant.",
"version": "4.0.1",
"version": "4.0.2",
"main": [
"./trueinstantclick.js"
],
Expand Down
56 changes: 36 additions & 20 deletions trueinstantclick.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,41 @@ var InstantClick = function(document, location) {

////////// MAIN FUNCTIONS //////////

function syncload(scripts, i) {
var script,
copy,
parentNode,
nextSibling;
if (i < scripts.length) {
script = scripts[i];
if (script.hasAttribute('data-no-instant')) {
syncload(scripts, i + 1);
return;
}
copy = document.createElement('script');
if (script.src) {
copy.src = script.src;
}
if (script.innerHTML) {
copy.innerHTML = script.innerHTML;
}
parentNode = script.parentNode;
nextSibling = script.nextSibling;
parentNode.removeChild(script);
parentNode.insertBefore(copy, nextSibling);
// real browsers:
copy.onload = function() {
syncload(scripts, i + 1);
};
// internet explorer:
copy.onreadystatechange = function() {
if (this.readyState == 'complete') {
syncload(scripts, i + 1);
}
}
}
}

function instantanize(isInitializing) {
var as = document.getElementsByTagName('a'),
a,
Expand All @@ -194,26 +229,7 @@ var InstantClick = function(document, location) {
a.addEventListener('click', click);
}
if (!isInitializing) {
var scripts = document.body.getElementsByTagName('script'),
script,
copy,
parentNode,
nextSibling;

for (i = 0, j = scripts.length; i < j; i++) {
script = scripts[i];
copy = document.createElement('script');
if (script.src) {
copy.src = script.src;
}
if (script.innerHTML) {
copy.innerHTML = script.innerHTML;
}
parentNode = script.parentNode;
nextSibling = script.nextSibling;
parentNode.removeChild(script);
parentNode.insertBefore(copy, nextSibling);
}
syncload(document.body.getElementsByTagName('script'), 0);
}
}

Expand Down

0 comments on commit 9a4fc2e

Please sign in to comment.