Obtaining query_string from JavaScript

M

Marcin

Hi All,

Let's say we have site index.html with JS embedded there in this
way:

<script type="text/javascript" src="my_script.js?username=mr_bean"></
script>

Question:
How to get access to "username" query string constant directly from
my_script.js file ? "location.search" is no option here because it
will access index.html's QUERY STRING not my_script.js's. Any idea ?

Best regards,
Marcin Zduniak
 
S

SAM

Le 9/24/08 5:15 PM, Marcin a écrit :
Hi All,

Let's say we have site index.html with JS embedded there in this
way:

<script type="text/javascript" src="my_script.js?username=mr_bean"></
script>

funny idea, no ?
Question:
How to get access to "username" query string constant directly from
my_script.js file ? "location.search" is no option here because it
will access index.html's QUERY STRING not my_script.js's. Any idea ?

var S = document.getElementsByTagName('script');
for(var i=0, n=S.length; i<n; i++)
if(S.src.toString().indexOf('my_script') >=0) {
alert('var = '+S.src.toString().split('=')[1]);
break;
}
 
T

Thomas 'PointedEars' Lahn

Marcin said:
Let's say we have site index.html with JS embedded there in this
way:

<script type="text/javascript" src="my_script.js?username=mr_bean"></
script>

Question:p
How to get access to "username" query string constant directly from
my_script.js file ? "location.search" is no option here because it
will access index.html's QUERY STRING not my_script.js's. Any idea ?

The script cannot "know" how it is included. However, you can use a
server-side script to generate a client-side one, and the former can
read the query part of the request URI.

For example, in my_script.js.php (I would use Content Negotiation to get
rid of the security-relevant `.php' in the URI):

var username = "<?php echo addslashes($_REQUEST['username']); ?>";


PointedEars
 
T

Thomas 'PointedEars' Lahn

Conrad said:
I wouldn't. I suppose content negotiation could also be (mis)used to
hide an extension, but it was really intended to serve alternate
representations of resources depending on the UA's capabilities and
preferences (different languages or media types). See RFC 2616.
URL rewriting would be more appropriate to hide or change file names.

URL rewriting cannot be reasonably applied to this problem: you would have
to write expressions for each script file (and have PHP parse all, which is
inefficient), or write an expression for all directories where you have to
keep all your generated script files (which is inflexible).

I might add that He who invented the Web and those who largely implemented
it disagree with you about the use of content negotiation as I do:

<http://www.w3.org/Provider/Style/URI>
<http://httpd.apache.org/docs/2.0/content-negotiation.html#naming>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top