November 5th, 2012, 11:00 PM
-
Dynamic data to javascript as query string?
Hi. I'm curious and I'm not sure but I wonder if you can pass data as query string when you define the src on the script tag?
Something like this?
src="myscripts.js?id=123"
Then inside the javascript it can actually use the id data? Is that possible?
If it's possible do you know how the code will look like?
Thank you.
November 6th, 2012, 02:27 PM
-
Yes, you can do this... you would do something like this:
externalJS.js
Code:
var myString = document.getElementsByTagName("script")[0].src.split("id=")[1];
alert(myString);
Add this above all of your script tags; so it will be the first script tag, that appears in your document.
HTML
Code:
<script type="text/javascript" src="externalJS.js?id=123"></script>