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.
The Code is as follows:
Copy codeThe 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:
Copy codeThe Code 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:
Copy codeThe Code is as follows:
<! 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>
Script house DEMO code http://demo.jb51.net/js/2011/jscc/