Page 1 of 1
[Solved]Passing a script a URL query string... Thingy.
Posted: Sat Feb 09, 2008 11:21 pm
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:
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.

) use?
Thanks for reading. =D
Re: Passing a script a URL query string... Thingy.
Posted: Sun Feb 10, 2008 1:39 am
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.
Re: Passing a script a URL query string... Thingy.
Posted: Sun Feb 10, 2008 3:24 am
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?
Re: Passing a script a URL query string... Thingy.
Posted: Sun Feb 10, 2008 3:36 am
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>
Re: Passing a script a URL query string... Thingy.
Posted: Sun Feb 10, 2008 3:41 am
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.
Re: [Solved]Passing a script a URL query string... Thingy.
Posted: Mon Feb 18, 2008 1:29 pm
by dayyanb
If you really want it to be a file ending with .js you can use mod_rewrite in .htaccess.