Passing parameters in JS files and processing skills

Source: Internet
Author: User

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. Today, I am not worried about this problem. I will summarize it today.

Solution:

1. first obtain the SRC attribute of the current JS file. Here 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.
The Code is as follows:
Var scripts = document. getElementsByTagName ("script ");
Var curJS = scripts [scripts. length-1]; // curJS is our current js File

 

You can use curJS. src to obtain the complete path content (including parameters ).

2. The following is the parsing parameter content. The parsing process is quite simple. I believe many people can easily complete this step.
However, we need to deal with a special case: If a parameter is input multiple times, we need to convert the value of this parameter to an array to store each input value.

The complete test script is as follows:

Var getArgs = (function (){
Var SC = document. getElementsByTagName ('script ');
Var paramsArr = SC [SC. length-1]. src. split ('? ') [1]. split ('&');
Var args ={}, argsStr = [], param, t, name, value;
For (var I = 0, len = paramsArr. length; I <len; I ++ ){
Param = paramsArr [I]. split ('= ');
Name = param [0], value = param [1];
If (typeof args [name] = "undefined") {// The parameter does not exist
Args [name] = value;
} Else if (typeof args [name] = "string") {// if the parameter already exists, it is saved as an array.
Args [name] = [args [name]
Args [name]. push (value );
} Else {// already an array
Args [name]. push (value );
}
}
/* The showArg and args. toString values in the actual application can be deleted. This is only to test the content returned by the getArgs function */
Var showArg = function (x) {// convert the display mode of different data
If (typeof (x) = "string "&&! // \ D +/. test (x) return "'" + x + "'"; // string
If (x instanceof Array) return "[" + x + "]" // Array
Return x; // number
}
// Assemble it into json format
Args. toString = function (){
For (var I in args) argsStr. push (I + ':' + showArg (args [I]);
Return '{' + argsStr. join (',') + '}';
}
Return function () {return args;} // return all obtained parameters in json format
})();

Alert (getArgs ());
Alert ("username:" + getArgs () ["username"]);

  

HTML source code of the test example:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> new document </title>
<Meta name = "generator" content = "editplus"/>
<Meta name = "author" content = ""/>
<Meta name = "keywords" content = ""/>
<Meta name = "description" content = ""/>
<Script type = "text/javascript" src = "test. js? Id = 4 & username = yemoo & id = 1 & uid = 110 "> </script>
<Script type = "text/javascript" src = "test. js? Id = 5 & username = ajaxbbs & id = 7 & uid = 253 "> </script>
<Script type = "text/javascript" src = "test. js? Id = 6 & username = jack & id = 8 & uid = 258 "> </script>
</Head>
<Body>
</Body>
</Html>

Article transferred from: http://www.cnitblog.com/yemoo/archive/2008/03/11/40799.html

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.