开发者

pass javascript variables

开发者 https://www.devze.com 2023-01-27 02:12 出处:网络
You can tell it\'s Sunday cos I\'m being \"thick\" today If I call an external .js file like this: <script type=\"text/javascript\" src=\"http://www.mydomain.com/script.js\"></script>

You can tell it's Sunday cos I'm being "thick" today

If I call an external .js file like this:

<script type="text/javascript" src="http://www.mydomain.com/script.js"></script>

Can I pass a variable at the same time something like this:

<script type="text/javascript" src="http://www.mydomain.com/script.js?variable_name='variable_value'></script>

If so, how do I "read" / use that variable on script.js?

OR even better - but I haven't got a clue about mod_rewrite would be:

<script type="text/javascript" src="http://www.mydomain.com/variable_value/script.js></script>

Then use a RewriteRule to strip the variable out, return script.js but still be able to use the variable value within script.开发者_如何学Pythonjs.

Any suggestions and help please thanks in advance.


JavaScript is loaded into a page and has no awareness of what URI was used to load it.

Your options are:

  1. Generate the JS server side and add in variables form the query string
  2. Grab (with the usual DOM methods) the last <script> element on the page and read it's src attribute (hoping that you aren't dealing with a defered script or one that has been injected before the end of the document)
  3. Just include any variables you want to pass as globals (or namespaced variables) in an inline script block before you src the external script. (This is what Google use with AdSense)
  4. As 3, but write the script to define a function which you call after loading the external script and pass it the variables.

Options 3 and 4 are safe, simple, non-hacky, cache friendly and generally the best options for most situations. Option 4 is the more elegant of the two.

If you want to use mod_rewrite to keep the variable out of the URI given to the browser, then option 2 won't work for you. If you are trying to hide the variable from the user options 3/4 won't work either (and looking at the JS src, if you go with option 1, will reveal it too).


Simple example without server support:

<!-- Init variable on a page before including the script: -->
<script type="text/javascript">
window.variable_name = { ... };
</script>

<!-- Use variable_name inside the script: -->
<script type="text/javascript" src="http://www.mydomain.com/script.js"></script>


You can, but it won't do what you think it does. Adding query string parameters will pass those parameters to the web server serving the script; if the javascript is served by some sort of server-side script, then that script can pick up the query string variables and do meaningful things with them.

The parameters won't magically show up in the javascript though, but you can do something much easier - just set global variables in a script somewhere before the include, and the included script will be able to see them. After all, all the scripts on your page share the same scripting context.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号