Get data attribute of script tag?

For modern browsers that support html5 you can use document.currentScript to get the current script element. Otherwise, use querySelector() to select your script element by id or attribute. Note that we don’t use the src attribute because that can be fragile if you’re delivering over a CDN or with differences between development and production environments. … Read more

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=”https://stackoverflow.com/questions/5292372/..” one=”1″ two=”2″></script> Inside above script: document.currentScript.getAttribute(‘one’); //1 document.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: … Read more

HTML/Javascript: how to access JSON data loaded in a script tag with src set

You can’t load JSON like that, sorry. I know you’re thinking “why I can’t I just use src here? I’ve seen stuff like this…”: <script id=”myJson” type=”application/json”> { name: ‘Foo’ } </script> <script type=”text/javascript”> $(function() { var x = JSON.parse($(‘#myJson’).html()); alert(x.name); //Foo }); </script> … well to put it simply, that was just the script … Read more

How to force a script reload and re-execute?

How about adding a new script tag to <head> with the script to (re)load? Something like below: <script> function load_js() { var head= document.getElementsByTagName(‘head’)[0]; var script= document.createElement(‘script’); script.src=”https://stackoverflow.com/questions/9642205/source_file.js”; head.appendChild(script); } load_js(); </script> The main point is inserting a new script tag — you can remove the old one without consequence. You may need to add … Read more