Js Code
$ (Function (){
// Obtain parameters from the page
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
})();
});
With this method, you can pass parameters to js from the page.
Html code
<Script src = ".../example. js? Id = 1 "type =" text/javascript "> </script>
For example, if you reference example. js on the page and pass the id with the parameter equal to 1, you can use getArgs () [id] In example. js to get the id value.
Author "fresh-daily"