Passing a variable to an external JS file?

JavaScript and client side scripting.

Moderator: General Moderators

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Passing a variable to an external JS file?

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Passing a variable to an external JS file?

Post 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).
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Passing a variable to an external JS file?

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Passing a variable to an external JS file?

Post 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">
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Passing a variable to an external JS file?

Post by superdezign »

Cleanliness, of course. :)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Passing a variable to an external JS file?

Post by AbraCadaver »

superdezign wrote:Cleanliness, of course. :)
And... One works and one doesn't. 8)
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Passing a variable to an external JS file?

Post 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
(#10850)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Passing a variable to an external JS file?

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Passing a variable to an external JS file?

Post 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>
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Passing a variable to an external JS file?

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Passing a variable to an external JS file?

Post 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/
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Passing a variable to an external JS file?

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Passing a variable to an external JS file?

Post by VladSun »

@Benjamin

It can't work in the OP case.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Passing a variable to an external JS file?

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Passing a variable to an external JS file?

Post 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?
Post Reply