I load this file with some query parameter as follows:
src='somefile.js?userId=123'
I wrote the following function in somefile.js file that reads userId request parameter
but I think this is not the best approach. Honestly, this is pretty ugly. Is there a better way ?
function getId(){ var scripts = document.getElementsByTagName('script'), script; for(var i in scripts){ if( scripts.hasOwnProperty(i) && scripts[i].src.indexOf('somefile.js') != -1 ) var script = scripts[i]; } var s = (script.getAttribute.length !== undefined) ? script.getAttribute('src') : script.getAttribute('src', 2); return getQueryParams('userId',s); };
vsync source share