jquery - Get parameter and value appended to the extension of .js file -
i'm working on site has js file included this:
<script type="text/javascript" src="script.js?param=value"></script> is there way can - inside script.js - param , value?
since script runs synchronously html parser, can last script element , read src:
var scripts = document.queryselectorall("script"); var script = scripts[scripts.length - 1]; console.log(script.src); // "script.js?param=value" then use string parsing grab what's after ?, break name/value pairs, etc.
or if use async or defer on script element , may not last one, 1 src contains script.js:
var script = document.queryselector("script[src*=script.js]"); console.log(script.src); // "script.js?param=value" this bit less reliable, since scripts can renamed. use queryselectorall("script") , find 1 parameters of sort expect.
for avoidance of doubt: don't recommend it. prefer pass options initialization call, or requiring object defined before script added (like google does), etc. can done.
Comments
Post a Comment