By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,796 Members | 1,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,796 IT Pros & Developers. It's quick & easy.

Obtaining query_string from JavaScript

P: n/a
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
Sep 24 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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[i].src.toString().indexOf('my_script') >=0) {
alert('var = '+S[i].src.toString().split('=')[1]);
break;
}
--
sm
Sep 24 '08 #2

P: n/a
Marcin wrote:
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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Sep 26 '08 #3

P: n/a
On 2008-09-26 20:24, Thomas 'PointedEars' Lahn wrote:
For example, in my_script.js.php (I would use Content Negotiation to get
rid of the security-relevant `.php' in the URI):
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.

- Conrad
Sep 26 '08 #4

P: n/a
Conrad Lender wrote:
On 2008-09-26 20:24, Thomas 'PointedEars' Lahn wrote:
>For example, in my_script.js.php (I would use Content Negotiation to get
rid of the security-relevant `.php' in the URI):

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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Sep 26 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.