Live data from Hacker News

Simple Way to Extract GET Params from a JavaScript Script Tag

loopj.com

21–26 of 26 posts

Re: Simple Way to Extract GET Params from a JavaScript Script Tag

#22
you can also do this with a regular expression. here's the blog post i wrote on it a year ago. i use one function to grab get variables from either window.location or from a script tag. 5 lines of code.

http://www.onlineaspect.com/2009/06/10/reading-get-variables...

Re: Simple Way to Extract GET Params from a JavaScript Script Tag

#23
post #18
post #12

This solution fails when you need to insert the script more than once in a page. Instead, browsers really should provide scripts a means to retrieve the script element that they belong to.

A comment on the article pointed out that since javascript execution is blocking, you can do that for synchronous javascript: scripts=document.elements.getElementByTagName('script') latest=scripts[scripts.length-1]

Right, but then you have to require that your script be loaded synchronously. And this is really just a hack to get around the fact that browsers don't provide a proper API.

Re: Simple Way to Extract GET Params from a JavaScript Script Tag

#24
post #14
post #12

This solution fails when you need to insert the script more than once in a page. Instead, browsers really should provide scripts a means to retrieve the script element that they belong to.

You can use a static counter to get around that

Sorry, can you explain what you mean?

Re: Simple Way to Extract GET Params from a JavaScript Script Tag

#25
post #15
post #8

Here's a simpler way. Put this in your htaccess file: AddHandler server-parsed .js then add: (function(query_string){ ... })(" "); around your script. This obviously has problems with caching, but it doesn't have the problems addressed with those "heavy loads" that you have with PHP et al.

Holy XSS injection, Batman!

QUERY_STRING isn't decoded. What am I missing here?

Re: Simple Way to Extract GET Params from a JavaScript Script Tag

#26
post #9
post #8

Here's a simpler way. Put this in your htaccess file: AddHandler server-parsed .js then add: (function(query_string){ ... })(" "); around your script. This obviously has problems with caching, but it doesn't have the problems addressed with those "heavy loads" that you have with PHP et al.

The beauty of doing this statically entirely in javascript is that you can serve the js file from anywhere, including s3 for example.

The beauty of doing this with SSI is that you can include the same script multiple times, but with different parameters, and get: different effects.
Post reply on HN