How to pass parameters to a Script tag?
I apologise for replying to a super old question but after spending an hour wrestling with the above solutions I opted for simpler stuff.
<script src=".." one="1" two="2"></script>
Inside above script:
document.currentScript.getAttribute('one'); //1document.currentScript.getAttribute('two'); //2
Much easier than jquery OR url parsing.
You might need the polyfil for doucment.currentScript from @Yared Rodriguez's answer for IE:
document.currentScript = document.currentScript || (function() { var scripts = document.getElementsByTagName('script'); return scripts[scripts.length - 1];})();
It's better to Use feature in html5 5 data Attributes
<script src="http://path.to/widget.js" data-width="200" data-height="200"></script>
Inside the script file http://path.to/widget.js you can get the paremeters in that way:
<script>function getSyncScriptParams() { var scripts = document.getElementsByTagName('script'); var lastScript = scripts[scripts.length-1]; var scriptName = lastScript; return { width : scriptName.getAttribute('data-width'), height : scriptName.getAttribute('data-height') }; }</script>
Got it. Kind of a hack, but it works pretty nice:
var params = document.body.getElementsByTagName('script');query = params[0].classList;var param_a = query[0];var param_b = query[1];var param_c = query[2];
I pass the params in the script tag as classes:
<script src="http://path.to/widget.js" class="2 5 4"></script>
This article helped a lot.