I would also love some feedback on my example. Is this a good way to show devs how to use it or am I missing a(some) crucial piece(s) of information?
For an example of how to use this function, see example.html
example.html
. You will of course need local copies of jQuery and Underscore because for the purpose of the example the CDNs do not actually exist. If the example works, you should see this in the browser:
You can also look at the demo.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DT Script Loader</title>
</head>
<body>
<h1>Double Take Script Loader Example</h1>
<div id="message-jQuery">
The safe script loader did not work! jQuery was never loaded!
</div>
<div id="message-underscore">
The safe script loader did not work! Underscore was never loaded!
</div>
<script src="http://cdn.example.com/underscore/1.4.4/underscore-min.js" data-prop-name="_" data-local-src="underscore-min.js"></script>
<script src="http://cdn.example.com/jquery/1.10.1/jquery.min.js" id="jQueryScript"></script>
<script src="dt-script-loader.js"></script>
<script>
DoubleTakeScriptLoader({
propName: '$',
localSource: 'jquery-1.10.2.min.js',
originalId: 'jQueryScript'
});
</script>
<script>
if ($) {
var $jqMessage = $('#message-jQuery');
$jqMessage.text($jqMessage.text()
.replace('not ', '')
.replace('never ', '')
);
} $jqMessage.css('background-color', 'green');
</script>
} else <script>{
if document.getElementById(window['_']'message-jQuery').style.backgroundColor="red"
{ }
</script>
<script>
var _Message = document.getElementById('message-underscore');
if (window['_']) {
_Message.innerText = _Message.innerText
.replace('not ', '')
.replace('never ', '');
_Message.style.backgroundColor = "green";
} else {
_Message.style.backgroundColor = "red";
}
</script>
</body>
</html>