[Solved]Passing a script a URL query string... Thingy.

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

[Solved]Passing a script a URL query string... Thingy.

Post by JellyFish »

I saw a post on Ajaxian once that showed an example of a document including a JavaScript file using the obvious script HTML element. But what was interesting, the script elements src attribute ended with "?bla=bla" (query string). As far as I know, if the script-being-included's content were:

Code: Select all

alert(location.search);
and if the src attribute were:

Code: Select all

<script type="text/javascript" src="[b]http://domain.com/dir/file.js?bla=bla[/b]"></script>
then the alert message wouldn't be "?bla=bla" unless if the document-including-the-script's query string was "?bla=bla".

So, I hope you get my point. What I'd like to know is how can a script react to the query string? The only way I can figure is to have the server parse the file before it's sent to the browser. But is there another way? What way did the example on Ajaxian (that I can't seem to find because Ajaxian post ten articles every hour of every day and the example was a couple months ago. :P) use?

Thanks for reading. =D
Last edited by JellyFish on Sun Feb 10, 2008 4:00 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Passing a script a URL query string... Thingy.

Post by Christopher »

I'm not sure what "how can a script react to the query string?" means. Usually the way it works it that Javascript passes parameters in the URL to PHP and PHP response with JSON which the <script> runs. Search for JSON and you will find examples.
(#10850)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Passing a script a URL query string... Thingy.

Post by JellyFish »

arborint wrote:I'm not sure what "how can a script react to the query string?" means. Usually the way it works it that Javascript passes parameters in the URL to PHP and PHP response with JSON which the <script> runs. Search for JSON and you will find examples.
Eeeeeeeeee-no... What I mean by saying "how can a script react to the query string?": let's say that I'd like to include a javascript file in my document. So I write:

Code: Select all

<script type="text/javascript" src="http://domain.com/dir/script.js"></script>
Okay. Now let's say this javascript file does something different depending on what parameters I pass to it using "?var=value". How can I achieve this?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Passing a script a URL query string... Thingy.

Post by Christopher »

JellyFish wrote:Okay. Now let's say this javascript file does something different depending on what parameters I pass to it using "?var=value". How can I achieve this?
Pass the parameters to a PHP script and have it generate the Javascript. Just like PHP is used as a HTML generator, it can also be a Javascript generator.

Code: Select all

<script type="text/javascript" src="http://domain.com/dir/script.php?var=value"></script>
(#10850)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Passing a script a URL query string... Thingy.

Post by JellyFish »

arborint wrote:
JellyFish wrote:Okay. Now let's say this javascript file does something different depending on what parameters I pass to it using "?var=value". How can I achieve this?
Pass the parameters to a PHP script and have it generate the Javascript. Just like PHP is used as a HTML generator, it can also be a Javascript generator.

Code: Select all

<script type="text/javascript" src="http://domain.com/dir/script.php?var=value"></script>

This is what I was thinking of doing, but my real question is how do I get the same results with a file that has a .js extension rather then .php.

I found an article on Ajaxian: http://ajaxian.com/archives/cnn-politic ... ion-center look at the second src attribute in the example. Notice how it's including "scriptaculous.js?load=effects" file and passing a parameter 'load' with the value 'effects'. How is this done? Do they just have the server watch this file; every time this file is requested by a client they parse it with a server side language like php? If this is the case, how do you get the server to parse a specific file with php ever time it is requested?

EDIT: Oh now I see. Scriptaculous actually looks at the script tag's src then loads the appropriate script. It doesn't actually dynamically generate it's self, at least not in the way I thought.

I think I got my answers to all my questions. Out.
dayyanb
Forum Commoner
Posts: 46
Joined: Wed Jan 23, 2008 12:34 am

Re: [Solved]Passing a script a URL query string... Thingy.

Post by dayyanb »

If you really want it to be a file ending with .js you can use mod_rewrite in .htaccess.
Post Reply