Passing parameters to js files and passing parameters to js files
In fact, passing parameters for js files is a problem that has been encountered for a long time, but has never been concerned about it. Let's sum up today.
Solution:
1. first obtain the SRC attribute of the current JS FileHere is a tip: we only need to get the last script TAG content on the current page.
Why ?? Because JS is parsed in sequence, the JS behind the current js script parsing has not been parsed yet. Of course, I think I am the last script. In addition, this method also provides the following benefits: We can reference the same file multiple times and input different parameters so that different processing can be performed based on different parameters in the js file! It's just a dynamic language.
var loadUrl = document.getElementsByTagName("script")[0].getAttribute("src");
You can get the complete path content (including parameters) through loadUrl ).
2. parse the parameter content.
function getCode(url,parm) {console.log(" url "+url); var reg = new RegExp("(^|&)"+ parm +"=([^&]*)(&|$)"); var r = url.substr(url.indexOf("\?")+1).match(reg); if (r!=null) return unescape(r[2]); return null; };var result = getCode.call(this,loadUrl,"userCode");alert(result);
3.
HTML source code of the test example:
<pre name="code" class="html"><!DOCTYPE html>
The output result is 1.
4. References
Http://blog.csdn.net/a9529lty/article/details/4168916