First, the use of global variables
This is one of the simplest ways, like Google Adsense:
1 <script type= "Text/javascript" >23 google_ad_client = ' pub-3741595817388494 ' ; 4 5 </script>67 <script type= "text/javascript" src= "http// Pagead2.googlesyndication.com/pagead/show_ads.js ">8 </script>
The disadvantage is the introduction of global variables. There are two variants of how files are introduced:
1 //Variant 1: Output with document.write2 3<script type= "Text/javascript" >4 5google_ga_id = ' g6u7un8646xx ';6 7document.write (unescape ('%3cscript type= "Text/javascript" src= "Http://www.google-analytics.com/ga.js"%3e%3c/ script%3e '));8 9</script>Ten One A - //Variant 2: Manipulating append into head with Dom - the<script type= "Text/javascript" > - -G_BEACON_ATP = ' category=&userid=&channel=112ad_id= '; - +document.getElementsByTagName (' head ') [0].appendchild (Document.createelement (' script ')]. src= ' Http://taobao.com/atp.js '; - +</script>
Note: Variant 1 applies a lot, and the common wording is as follows:
1<script type= "Text/javascript" >2 3 //Direct Escape can:4 5document.write (' <script type= ' text/javascript ' src= ' test.js ' ></script> ');6 7 8 9 //or just like Yahoo! Home page:Ten Onedocument.write (' <scr ' + ' IPT type= "Text/javascript" src= "test.js" >/scr ' + ' ipt> '); A -</script>
Second, get and parse the src of the script element
Compared with all variables, we would prefer to pass in parameters like this:
1 <script type= "Text/javascript" src= "Test.js?a=b&c=d" ></script>
The core issue is how to get to the SRC attribute. The first method is to add the id attribute to the script, get the current script by ID, and then take the parameter out of SRC with the regular. The disadvantage is that the script element does not have an id attribute in HTML 4.01 specification. This shortcoming is also not a disadvantage, after all, the standard is not as good as standard. Method Two is to use the JS filename as a hook, JS code through the document.getElementsByTagName (' script '), the regular match the current JS file. This method is Orthodox, but requires a unique file name. The disadvantage is that the code is much, not refined, and has a slight impact on performance. Method Three is on the basis of method one, simply add a custom attribute data:
1 <script id= "Testscript" type= "Text/javascript" src= "Test.js" Data= "A=b&c=d" ></script>
In the Test.js file, the parameters passed in the following line are obtained:
1 var scriptargs = document.getElementById (' Testscript '). getattribute (' data ');
Method Four is the use of JS sequential execution mechanism (JS file loading can be synchronous or asynchronous mode, but when executed, must be performed in the order of the document flow). When a JS file executes, it must be the last one in the "Loaded" JS file:
1 var scripts = document.getelementsbytagname (' script '); 2 3 var currentscript = scripts[scripts.length-1];
Method Four is more dexterous than the method two genius. From the simplification and performance of the Code, method three > method one > Method four > method Two summary: If you care about the standard, recommend method four; If you feel that you do not need to comply fully with the standard, recommend method three.
Third, the Inspiration program
If you're like me, John Resig's loyal fans, you might remember the degrading Script Tags, which was a hot discussion last August. John Resig opened a door for us to imagine, for the problem of this article, you can also use the following "devious" to achieve:
1 <script type= "Text/javascript" src= "Test.js" >23 TB. Someapp.scriptargs = ' A=b&c=d '; 4 5 </script>
In the Test.js file:
1 TB = {}; TB. Someapp = {}; 2 3 var scripts = document.getElementsByTagName ("script"); 4 5 eval (scripts[scripts.length-1].innerhtml);
This stores the parameters in the TB.SomeApp.scriptArgs variable. When the parameters are not long, you can even do this:
1 <script type= "Text/javascript" src= "Test.js" >a=b&c=d</script>
JS file:
1 var scripts = document.getElementsByTagName ("script"); 2 3 var scriptargs = scripts[scripts.length-1].innerhtml.replace (/[s]/g, ');
Imagination is endless, and the onload can be used:
1 <script type= "Text/javascript" src= "Test.js" onload= "TB. Somefun (' A=b&c=d ') "></script>
JS file, you can define a good function:
1 TB = {}; 2 3 function // code};
The above code works correctly in non-IE browsers. For the stupid ie, you have to add a few lines of code:
1 if (window. ActiveXObject) {23 var scripts = document.getelementsbytagname (' script '); 4 5 Eval (Scripts[scripts.length-1].getattribute (' onload ')); 6 7 }
This article was previously linked to: http://www.oschina.net/question/574208_122713
Several ways to pass in parameters to JavaScript files