SEO Forums: Your seo discussion forum  
Welcome, Unregistered.
You last visited: Today at 05:05 AM
Tags:



Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-25-2008, 04:55 AM
Marcin
Guest
 
Posts: n/a
Default Obtaining query_string from JavaScript



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
Reply With Quote


  #2 (permalink)  
Old 09-25-2008, 04:55 AM
SAM
Guest
 
Posts: n/a
Default Re: Obtaining query_string from JavaScript

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
Reply With Quote


  #3 (permalink)  
Old 09-26-2008, 08:31 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default Re: Obtaining query_string from JavaScript

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
> 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, <f806at$ail$1$8300dec7@news.demon.co.uk>
Reply With Quote


  #4 (permalink)  
Old 09-26-2008, 08:31 PM
Conrad Lender
Guest
 
Posts: n/a
Default Re: Obtaining query_string from JavaScript

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
Reply With Quote


  #5 (permalink)  
Old 09-27-2008, 07:22 AM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default Re: Obtaining query_string from JavaScript

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, <f806at$ail$1$8300dec7@news.demon.co.uk>
Reply With Quote


  #6 (permalink)  
Old 09-27-2008, 07:23 AM
Conrad Lender
Guest
 
Posts: n/a
Default Re: Obtaining query_string from JavaScript

On 2008-09-26 21:49, Thomas 'PointedEars' Lahn wrote:
> 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).


How many script files do you want to generate for each request? In
practically all of the projects that I've worked on, there was no need
to have more than one external JavaScript file (if any) that contained
dynamic information. That one script would usually be very short and
only contain a few custom settings that were used as "constants" by the
rest of the (static and cacheable) script files. More likely, these
definitions would not go into an external script file at all, but would
be added inline in the <head> of the HTML page (saving one request).

There may be situations where modules in a framework each want to add
their own dynamic script file. But in this case, there is usually a
single point of entry anyway (an /index.php file, for example) which
would take care of generating any dynamic content based on the PATH_INFO
env, without any need for content negotiation or URL rewriting.

> 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>


But they do not disagree. In fact, they don't compare URL rewriting and
content negotiation at all. The first reference is indeed interesting
(written by Sir Tim, no less), but *very* old. The introduction states:

| This document was written in the early days of the web (1992),
| defining such terms as "webmaster", the "www.xxx.com" convention, and
| a few basic points which are just as valid today. It has not been
| updated to discuss recent developments in HTML., and is out of date in
| many places, except for the addition of a few new pages, with given
| dates.
....
| Some of the points made may be influenced by personal preference, and
| some may be common sense, but a collection of points has been
| demanded, and so here it is.

Rewrite engines have been around for quite a while now, and there's
hardly a major project where URL rewriting of some sort isn't heavily
used. Sure, it may be a little trickier to set up than content
negotiation, but it's well worth learning.

I'm not sure why you included the second link, since it explicitly
states that content negotiation is intended to let the server "choose
the best representation of a resource based on the browser-supplied
preferences for media type, languages, character set and encoding." The
examples in the fragment you linked to are language and gzip compression
- both prime examples of switching by UA preferences and capabilities.

Anyway, the point is moot. It seems like all of our discussions tend to
end in YMMV anyway (I guess Perl doesn't have a monopoly on more than
one way of doing things ;-). I usually go with the solution that was
designed for a certain problem or situation; other solutions may work as
well, but in this case it feels like a bit of a hack.


- Conrad
Reply With Quote


Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 05:05 AM.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
Great Seo Blog at SEONOTEPAD.COM