Last active
December 16, 2015 07:49
-
-
Save prettycode/5401842 to your computer and use it in GitHub Desktop.
Module loader for testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var example = { | |
//js: 'https://gist.github.com/prettycode/5401842/raw/example.js' | |
js: 'http://fiddle.jshell.net/js/heyoffline.js?StillNoSpring' | |
} | |
loadScript(example.js, { | |
document: { | |
createElement: function(tag) { | |
return { | |
setAttribute: function(key, value) { | |
this._attribute[key] = value; | |
}, | |
submit: function() { | |
if (this._attribute['action'] !== example.js) { | |
throw; | |
} | |
} | |
}; | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this is the script that needs to be tested | |
function submitForm(url, data) { | |
var formEl = document.createElement('form'); | |
for (var key in data) { | |
if (data.hasOwnProperty(key)) { | |
formEl.setAttribute(key, data[key]); | |
} | |
} | |
formEl.action = url; | |
formEl.submit(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function loadScript(url, context) { | |
var scriptEl = document.createElement('script'), | |
contextKeys = Object.keys(context).join(); | |
jQuery.get(url, function(scriptText) { | |
var tempKey = '$' + Date.now(); | |
window[tempKey] = Object.keys(context).map(function(key) { | |
return context[key]; | |
}); | |
scriptEl.innerHTML = '' + | |
'(function(' + contextKeys + ') {' + | |
scriptText + | |
'}).apply(this, window.' + tempKey + ');' | |
; | |
document.body.appendChild(scriptEl); | |
delete window[tempKey]; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment