Forum Moderators: open
<script type="text/javascript" src="myscript.js"></script>
I'd like to know if it's possible to pass an argument to that script file, and if so, how do I read it in the external file? Something like this perhaps:?
<script type="text/javascript" src="myscript.js?id=123"></script>
Then in the JavaScript file, I need some way to get the value of "id".
Thoughts?
<script type="text/javascript">
var id = 123;
</script>
<script type="text/javascript" src="myscript.js"></script>
Indeed. This <post #2> is probably the most straightforward thing to do (depending on your actual needs, of course).
A more complicated approach is to serve the script file dynamically.
A bare bones ASP example:
--- TEST PAGE ---
<script src="script.asp?name=brian" type="text/javascript"></script> --- script.asp ---
alert("[blue]<%= Request.QueryString("name") %>[/blue]") -------------------
I tested this on IIS, with Internet Explorer. Stricter browsers may require you to play with content headers in the ASP file.
Thanks.
var someControl = new MyControl({
setting1: "value1",
setting2: "value2"
});
In this JS file:
function MyControl(params) {
this = params;
... do other stuff...
}
Depending on your tool and application of course...
- JS