Passing parameters to js files and passing parameters to js files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.