Home  >  Article  >  Web Front-end  >  JS file transfer and processing skills analysis_javascript skills

JS file transfer and processing skills analysis_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:27:25995browse

Solution:

1. First get the SRC attribute of the current JS file . Here is a little trick: we only need to get the last script tag content of the current page.
Why? ? Because JS is parsed sequentially, when the current JS script is parsed, the following js have not been parsed yet, so of course it is considered to be the last script. In addition, there is another benefit to obtaining it this way: we can reference the same file multiple times and pass in different parameters, so that we can do different processing according to different parameters in the js file, which is very clever! It is simply a dynamic language.
The code is as follows:

Copy code The code is as follows:

var scripts=document.getElementsByTagName ("script");
var curJS=scripts[scripts.length-1]; //curJS is our current js file

It’s easy to get this, through curJS.src You can get 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.
But we have to deal with a special case: if a parameter is passed in multiple times, the parameter value must be converted into an array to store each passed in value.

The complete test script is as follows:

Copy the code The 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;iparam=paramsArr[i].split('=');
name=param[0],value=param[1];
if(typeof args[name]== "undefined"){ //The parameter does not exist yet
args[name]=value;
}else if(typeof args[name]=="string"){ //The parameter already exists, then save it as an array
args[name]=[args[name]]
args[name].push(value);
}else{ //Already an array
args[name].push(value) ;
}
}
/*In actual applications, the following showArg and args.toString can be deleted. This is just to test the content returned by the function getArgs*/
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 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 test example:
Copy code The code is as follows:



< head>
new document












Script House demo codehttp: //demo.jb51.net/js/2011/jscc/
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn