Page 1 of 2
Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 8:32 am
by Luke
Let me know if this is possible... I know I've seen GET type variables being passed to javascript files, but I don't know how to access them or how it works. I am almost positive I've seen stuff like this:
Code: Select all
<script type="text/javascript" src="/js/some-js-file.js?var=foo">
But I've never seen how that variable might be accessed from within the javascript file. Is it possible? Or when I've seen this, are they just rendering js files with php or what?
Re: Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 9:09 am
by VladSun
I think it's server side (e.g. by handling .js files with PHP).
There is a way to access GET variables but only for a window/frame by using its location property (and I suppose you know it).
Re: Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 3:03 pm
by Jonah Bron
Yeah, I think you'd have to add .js to you list of PHP-evaluated file extensions. Either that or use .php extension for the JS file?
Re: Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 3:17 pm
by AbraCadaver
Luke wrote:Let me know if this is possible... I know I've seen GET type variables being passed to javascript files, but I don't know how to access them or how it works. I am almost positive I've seen stuff like this:
Code: Select all
<script type="text/javascript" src="/js/some-js-file.js?var=foo">
But I've never seen how that variable might be accessed from within the javascript file. Is it possible? Or when I've seen this, are they just rendering js files with php or what?
With a javascript file no. But as others have stated you can use a PHP file that outputs the javascript, but what is the difference between what you posted and this?
Code: Select all
<script type="text/javascript">
var myVar = 'foo';
</script>
<script type="text/javascript" src="/js/some-js-file.js">
Re: Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 3:37 pm
by superdezign
Cleanliness, of course.

Re: Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 3:40 pm
by AbraCadaver
superdezign wrote:Cleanliness, of course.

And... One works and one doesn't.

Re: Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 3:47 pm
by Christopher
You can have a PHP file generate your script -- and that can be passed parameters. Or you may be looking for this:
http://www.xml.com/pub/a/2005/12/21/jso ... t-tag.html
Re: Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 4:46 pm
by pickle
~AbraCadaver's solution is the cleanest as far as I'm concerned. You're using existing techniques to do the work, rather than hacking your web server, or using PHP to output Javascript.
Re: Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 5:09 pm
by VladSun
pickle wrote:~AbraCadaver's solution is the cleanest as far as I'm concerned. You're using existing techniques to do the work, rather than hacking your web server, or using PHP to output Javascript.
I usually have one <script> tag:
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Ext Dev</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="/css" />
<script type="text/javascript" src="/scripts"></script>
</head>
<body>
</body>
</html>
A simple index.php in the /css/ and /javascript/ directories do the rest.
No web server hacking (i.e. handling *.js with PHP). I mean - directory index options are easily exploited to handle such things without reconfiguring the web server.
It will handle even this:
Code: Select all
<script type="text/javascript" src="/scripts/?var=value"></script>
Re: Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 5:19 pm
by pickle
True, but you have to set all that up. Granted, you may have that already in a framework, but it's still more work than just adding a script tag. That's what they're there for. That's why you can do that. With utmost respect, it seems a bit like re-inventing the wheel.
Re: Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 5:27 pm
by Benjamin
[Edit] Found a better solution:
Code: Select all
// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
Copied from:
http://snipplr.com/view/799/get-url-variables/
Re: Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 5:28 pm
by VladSun
@pickle
It really depends on what the "?var=value" thing is going to be used for. If it's a simple PHP to JS variables populating, then it doesn't worth it, indeed.
Otherwise, it can be very useful.
Re: Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 5:31 pm
by VladSun
@Benjamin
It can't work in the OP case.
Re: Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 9:29 pm
by Benjamin
VladSun wrote:@Benjamin
It can't work in the OP case.
Ok, so assuming that window.location is the location of the requested document, instead of the javascript file that was requested, is there any way to obtain the URI of the javascript file that was requested?
Re: Passing a variable to an external JS file?
Posted: Wed Dec 15, 2010 9:53 pm
by Weirdan
Benjamin wrote:
Ok, so assuming that window.location is the location of the requested document, instead of the javascript file that was requested, is there any way to obtain the URI of the javascript file that was requested?
Searching through DOM for corresponding <script> tag and reading its src attribute?