Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to a Script tag?

I read the tutorial DIY widgets - How to embed your site on another site for XSS Widgets by Dr. Nic.

I'm looking for a way to pass parameters to the script tag. For example, to make the following work:

<script src="http://path/to/widget.js?param_a=1&amp;param_b=3"></script> 

Is there a way to do this?


Two interesting links:

  • How to embed Javascript widget that depends on jQuery into an unknown environment (Stackoverflow discussion)
  • An article on passing parameters to a script tag
like image 571
Tomer Lichtash Avatar asked Mar 13 '11 21:03

Tomer Lichtash


People also ask

How do you pass a value in a script?

Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.

How do you pass variables between JavaScript files?

You cannot pass variables from one js file to the other Javascript variables are stateless so they wont be retained. If you are using . Net then you can make use of Session variables to solve this purpose. If you use MVC you can go for viewbag or viewdata.

How pass variable parameter in JavaScript function from HTML?

Use the onclick attribute in a button tag with the function name and pass value in this function. With this method, you can also take input from users and pass parameters in the JavaScript function from HTML.


1 Answers

I apologise for replying to a super old question but after spending an hour wrestling with the above solutions I opted for simpler stuff.

<script src=".." one="1" two="2"></script> 

Inside above script:

document.currentScript.getAttribute('one'); //1 document.currentScript.getAttribute('two'); //2 

Much easier than jquery OR url parsing.

You might need the polyfil for doucment.currentScript from @Yared Rodriguez's answer for IE:

document.currentScript = document.currentScript || (function() {   var scripts = document.getElementsByTagName('script');   return scripts[scripts.length - 1]; })(); 
like image 93
ProbablePrime Avatar answered Sep 21 '22 18:09

ProbablePrime