Upload parameters to the js file (detailed description ).
I. Use global variables
This is the simplest way, such as Google Adsense:
Copy codeThe Code 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 variants in the file import method:
// Variant 1: Use document. write output <script type = "text/javascript"> google_ga_id = 'g6u7un8646xx '; document. write (unescape ('% 3 Cscript type = "text/javascript" src = "http://www.google-analytics.com/ga.js" % 3E % 3C/script % 3E ')); </script> // Variant 2: use DOM to append to the head. <script type = "text/javascript"> G_BEACON_ATP = 'category = & userid = & channel = upload_id = '; document. getElementsByTagName ('head') [0]. appendChild (document. createElement ('script ')). src = 'HTTP: // taobao.com/atp.js'; </script> // Note: The above code is a Demo code based on the actual application.
Note: Variant 1 has many applications. The common syntax is as follows:
<Script type = "text/javascript"> // escape directly: document. write ('<script type = "text/javascript" src = "test. js "> </script> '); // or like Yahoo! Home page: document. write ('<scr' + 'ipt type = "text/javascript" src = "test. js "> </scr '+ 'ipt'); </script>
2. Obtain and parse the src of the script Element
Compared with all variables, we want to pass in parameters as follows:
<Script type = "text/javascript" src = "test. js? A = B & c = d "> </script>
The core issue is how to obtain the src attribute.
The first method is to add the id attribute to the script, get the current script through the id, and then retrieve the parameter from src using the regular expression. The disadvantage is that the SCRIPT element does not have the id attribute in HTML 4.01 Specification. This disadvantage is not a disadvantage. After all, it is better to believe in standards than to have no standards.
The second method is to use the js file name as a hook. After using document. getElementsByTagName ('script') in js Code, the regular expression matches the current js file. This method is orthodox, but the file name must be unique. The disadvantage is that there are many codes that are not refined, which also has a slight impact on performance.
Method 3 adds 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 obtain the input parameters:
Var scriptArgs = document. getElementById ('testscript '). getAttribute ('data'); Method 4: Use the js sequential execution mechanism (js file loading can be synchronous or asynchronous, but during execution, it must be executed in the order in the Document Stream ). When a 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 smarter than method 2.
In terms of code streamlining and performance, method 3> method 1> Method 4> method 2
Summary: If you are very concerned about standards, recommendation method 4 is recommended. If you feel like I do not need to fully abide by the standards, recommendation method 3 is recommended.
I wrote a test program.
<!DOCTYPE html>
A2.js
Var scripts = document. getElementsByTagName ('script'); var currentScript = scripts. length; alert (currentScript );
Print out
1 2 3
3. Inspiration plan
If you are like me, John Resig's loyal fans, you may still remember the popular Degrading Script Tags discussed in last August. John Resig opened an imaginary door for us. For the problems in this article, we can also use the following "bad ideas:
<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);
In this way, the parameter is stored 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, '');
There is no end to it. You can also use onload:
<Script type = "text/javascript" src = "test. js" onload = "TB. SomeFun ('a = B & c = D')"> </script>
Define the function in the js file:
TB = {}; TB.SomeFun = function(arg) { //code};
The above code runs correctly in non-ie browsers. For 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'));}
Js file Parameters
The parameters of the js file are parsed by the server, not by window. location. search, as hbxflzh users call it. It means to use js to obtain the parameters passed in the webpage, which is the client.
In fact, many server scripts can specify the last name of the file or the last name. For example, apache can resolve the js file to php through configuration, of course, the actual server code in your js file should be php code and js Code should be output through php code. In this way, you can use *. js? Param1 = p1 & param2 = p2: input parameters to the server. Then, the server returns different JS Code to the browser based on the parameters. Then, the Browser executes the returned js Code.
Yang yunjun [authoritative expert]
Whether parameters can be passed to js files
If it is static, it does not seem to work, but for dynamic websites, you can set the file suffix to participate in the interpretation and add *. js.