get js file query param from inside it
<p>I load this file with some query param like this:<br/>
<code><strong>src='somefile.js?userId=123'</strong></code></p>

<p>I wrote the below function in 'somefile.js' file that reads the 'userId' query param<br/>
but I feel this is not the best approach. Frankly, its quite ugly. <strong>Is there a better way</strong>?</p>

<pre><code>function getId(){
   var scripts = document.getElementsByTagName('script'), script;
   for(var i in scripts){
        if( scripts.hasOwnProperty(i) &amp;&amp; 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);
};
</code></pre>
