Skip to main content
Removed "." in script tags.
Source Link
Paul
  • 4k
  • 2
  • 21
  • 38

I like the look of this in general. Its short, simple and I'd love to know how well its worked in the field.

I'm not a big fan of document.write but I don't think the alternative is much better. If you look in the jQuery source code you'll see they eval the script.

It comes from http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context

In place of the document.write I might put something like:

var xmlHttpReq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlHttpReq.open('GET', localSource);
xmlHttpReq.setRequestHeader('Content-Type', 'application/javascript');
xmlHttpReq.onreadystatechange = function _readyStateChange() {
    if (xmlHttpReq.readyState == 4) {
        (window.execScript || window.eval).call(window, xmlHttpReq.responseText);
    }
}
xmlHttpReq.send(null);

This uses an ajax request to get the JavaScript instead of blocking your page load. Its all a matter of taste I believe. (Also any other scripts might fail if they are expecting your framework to have loaded.)


After some Google Searching I've found a few places that do the almost same as your framework in less code.

<scrip.t<script src="/path/to/cdn/framwork.js"></scrip.t>script>
<scrip.t><script> window.framework || document.write("<scrip.t"<script src=\"/local/path/framework.js\"></scrip.t>"script>");</scrip.t>script>

It is shorter but requires one per script file.

I like the look of this in general. Its short, simple and I'd love to know how well its worked in the field.

I'm not a big fan of document.write but I don't think the alternative is much better. If you look in the jQuery source code you'll see they eval the script.

It comes from http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context

In place of the document.write I might put something like:

var xmlHttpReq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlHttpReq.open('GET', localSource);
xmlHttpReq.setRequestHeader('Content-Type', 'application/javascript');
xmlHttpReq.onreadystatechange = function _readyStateChange() {
    if (xmlHttpReq.readyState == 4) {
        (window.execScript || window.eval).call(window, xmlHttpReq.responseText);
    }
}
xmlHttpReq.send(null);

This uses an ajax request to get the JavaScript instead of blocking your page load. Its all a matter of taste I believe. (Also any other scripts might fail if they are expecting your framework to have loaded.)


After some Google Searching I've found a few places that do the almost same as your framework in less code.

<scrip.t src="/path/to/cdn/framwork.js"></scrip.t>
<scrip.t> window.framework || document.write("<scrip.t src=\"/local/path/framework.js\"></scrip.t>");</scrip.t>

It is shorter but requires one per script file.

I like the look of this in general. Its short, simple and I'd love to know how well its worked in the field.

I'm not a big fan of document.write but I don't think the alternative is much better. If you look in the jQuery source code you'll see they eval the script.

It comes from http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context

In place of the document.write I might put something like:

var xmlHttpReq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlHttpReq.open('GET', localSource);
xmlHttpReq.setRequestHeader('Content-Type', 'application/javascript');
xmlHttpReq.onreadystatechange = function _readyStateChange() {
    if (xmlHttpReq.readyState == 4) {
        (window.execScript || window.eval).call(window, xmlHttpReq.responseText);
    }
}
xmlHttpReq.send(null);

This uses an ajax request to get the JavaScript instead of blocking your page load. Its all a matter of taste I believe. (Also any other scripts might fail if they are expecting your framework to have loaded.)


After some Google Searching I've found a few places that do the almost same as your framework in less code.

<script src="/path/to/cdn/framwork.js"></script>
<script> window.framework || document.write("<script src=\"/local/path/framework.js\"></script>");</script>

It is shorter but requires one per script file.

added 351 characters in body
Source Link
James Khoury
  • 3.1k
  • 1
  • 24
  • 51

I like the look of this in general. Its short, simple and I'd love to know how well its worked in the field.

I'm not a big fan of document.write but I don't think the alternative is much better. If you look in the jQuery source code you'll see they eval the script.

It comes from http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context

In place of the document.write I might put something like:

var xmlHttpReq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlHttpReq.open('GET', localSource);
xmlHttpReq.setRequestHeader('Content-Type', 'application/javascript');
xmlHttpReq.onreadystatechange = function _readyStateChange() {
    if (xmlHttpReq.readyState == 4) {
        (window.execScript || window.eval).call(window, xmlHttpReq.responseText);
    }
}
xmlHttpReq.send(null);

This uses an ajax request to get the JavaScript instead of blocking your page load. Its all a matter of taste I believe. (Also any other scripts might fail if they are expecting your framework to have loaded.)


After some Google Searching I've found a few places that do the almost same as your framework in less code.

<scrip.t src="/path/to/cdn/framwork.js"></scrip.t>
<scrip.t> window.framework || document.write("<scrip.t src=\"/local/path/framework.js\"></scrip.t>");</scrip.t>

It is shorter but requires one per script file.

I like the look of this in general. Its short, simple and I'd love to know how well its worked in the field.

I'm not a big fan of document.write but I don't think the alternative is much better. If you look in the jQuery source code you'll see they eval the script.

It comes from http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context

In place of the document.write I might put something like:

var xmlHttpReq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlHttpReq.open('GET', localSource);
xmlHttpReq.setRequestHeader('Content-Type', 'application/javascript');
xmlHttpReq.onreadystatechange = function _readyStateChange() {
    if (xmlHttpReq.readyState == 4) {
        (window.execScript || window.eval).call(window, xmlHttpReq.responseText);
    }
}
xmlHttpReq.send(null);

This uses an ajax request to get the JavaScript instead of blocking your page load. Its all a matter of taste I believe. (Also any other scripts might fail if they are expecting your framework to have loaded.)

I like the look of this in general. Its short, simple and I'd love to know how well its worked in the field.

I'm not a big fan of document.write but I don't think the alternative is much better. If you look in the jQuery source code you'll see they eval the script.

It comes from http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context

In place of the document.write I might put something like:

var xmlHttpReq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlHttpReq.open('GET', localSource);
xmlHttpReq.setRequestHeader('Content-Type', 'application/javascript');
xmlHttpReq.onreadystatechange = function _readyStateChange() {
    if (xmlHttpReq.readyState == 4) {
        (window.execScript || window.eval).call(window, xmlHttpReq.responseText);
    }
}
xmlHttpReq.send(null);

This uses an ajax request to get the JavaScript instead of blocking your page load. Its all a matter of taste I believe. (Also any other scripts might fail if they are expecting your framework to have loaded.)


After some Google Searching I've found a few places that do the almost same as your framework in less code.

<scrip.t src="/path/to/cdn/framwork.js"></scrip.t>
<scrip.t> window.framework || document.write("<scrip.t src=\"/local/path/framework.js\"></scrip.t>");</scrip.t>

It is shorter but requires one per script file.

Source Link
James Khoury
  • 3.1k
  • 1
  • 24
  • 51

I like the look of this in general. Its short, simple and I'd love to know how well its worked in the field.

I'm not a big fan of document.write but I don't think the alternative is much better. If you look in the jQuery source code you'll see they eval the script.

It comes from http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context

In place of the document.write I might put something like:

var xmlHttpReq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlHttpReq.open('GET', localSource);
xmlHttpReq.setRequestHeader('Content-Type', 'application/javascript');
xmlHttpReq.onreadystatechange = function _readyStateChange() {
    if (xmlHttpReq.readyState == 4) {
        (window.execScript || window.eval).call(window, xmlHttpReq.responseText);
    }
}
xmlHttpReq.send(null);

This uses an ajax request to get the JavaScript instead of blocking your page load. Its all a matter of taste I believe. (Also any other scripts might fail if they are expecting your framework to have loaded.)