1. Utilize global variables
This is the easiest way, like Google Adsense:
The code copy is as follows:
<script type="text/javascript"> google_ad_client ='pub-3741595817388494'; </script> <script type="text/javascript" src="http://pagead2. googlesyndication.com/pagead/show_ads.js"></script>
The disadvantage is that global variables are introduced. There are two other variants of the way to introduce files:
// Variation 1: Use document.write to output <script type="text/javascript"> google_ga_id ='g6u7un8646xx'; document.write(unescape('%3Cscript type="text/javascript" src= "http://www.google-analytics.com/ga.js"%3E%3C/script%3E')); </script> // Variation 2: Use DOM to apply to head<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: The above code is a virtual demonstration code based on actual applicationNote: Variety 1 is used many times, and the common writing methods are as follows:
<script type="text/javascript"> // Escape directly: document.write('<script type="text/javascript" src="test.js"></script>'); // Or like Yahoo! homepage: document.write('<scr'+'ipt type="text/javascript" src="test.js"></scr'+'ipt>');</script>2. Get and parse the src of the script element
Compared with all variables, we prefer to pass parameters like the following:
<script type="text/javascript" src="test.js?a=b&c=d"></script>
The core problem is how to get the src attribute.
The first method is to add an id attribute to the script, obtain the current script through id, and then use regular to remove the parameters from src. The disadvantage is that in HTML 4.01 Specification, the SCRIPT element has no id attribute. This shortcoming cannot be considered a shortcoming, after all, it is better to have no standards than to believe in standards.
The second method is to use the js file name as a hook. After passing document.getElementsByTagName('script') in the js code, the current js file will be matched regularly. This method is orthodox, but requires a unique file name. The disadvantage is that there is a lot of code and is not refined, which will have a slight impact on performance.
Method 3 is to add a custom attribute data based on method 1:
<script id="testScript" type="text/javascript" src="test.js" data="a=b&c=d"></script>
In the test.js file, use the following line to get the passed parameters:
var scriptArgs = document.getElementById('testScript').getAttribute('data'); Method 4 is to use the sequential execution mechanism of js (the loading of js files can be synchronous or asynchronous, but when executing, it must be executed in the order in the document stream). When a certain js file is executed, it must be the last one in the "loaded" js file:
var scripts = document.getElementsByTagName('script'); var currentScript = scripts[scripts.length - 1]; Method 4 is more clever and genius than method 2.
From the perspective of code simplification and performance, Method 3 > Method 1 > Method 4 > Method 2
Summary: If you care about the standards very much, recommend method 4; if you feel that it is not necessary to fully comply with the standards like me, recommend method 3.
Write a test program
<!DOCTYPE html><html><script src="a2.js"></script><script src="a2.js"></script><script src="a2.js"></script><script src="a2.js"></script></html>
a2.js
var scripts = document.getElementsByTagName('script'); var currentScript = scripts.length;alert(currentScript);
Print out separately
1 2 3
3. Inspiration plan
If you are John Resig's loyal fans like me, maybe you still remember the "Degrading Script Tags" that was discussed very much last August. John Resig opens an imaginary door for us. For the questions in this article, we can also use the following "evil paths" to achieve it:
<script type="text/javascript" src="test.js"> TB.SomeApp.scriptArgs ='a=b&c=d'; </script>
In the test.js file:
TB = {}; TB.SomeApp = {}; var scripts = document.getElementsByTagName("script");eval(scripts[ scripts.length - 1 ].innerHTML);This stores the parameters in the TB.SomeApp.scriptArgs variable.
When there are not many parameters, you can even do this:
<script type="text/javascript" src="test.js">a=b&c=d</script>
In the js file:
var scripts = document.getElementsByTagName("script"); var scriptArgs = scripts[ scripts.length - 1 ].innerHTML.replace(/[s]/g, '');Imagination is endless, you can also use onload:
<script type="text/javascript" src="test.js" onload="TB.SomeFun('a=b&c=d')"></script>
Just define the function in the js file:
TB = {}; TB.SomeFun = function(arg) { //code};The above code can run correctly in non-IE browsers. For the stupid ie, you have to add a few lines of code:
if(window.ActiveXObject) { var scripts = document.getElementsByTagName('script'); eval(scripts[scripts.length - 1].getAttribute('onload'));}