Live data from Hacker News

Simple Way to Extract GET Params from a JavaScript Script Tag

loopj.com

1–10 of 26 posts

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

#3
post #2

Doesn't support multiple keys with the same name, which is valid and not particularly uncommon. I wouldn't be surprised if it breaks in other ways. This kind of thing is always full of subtle pitfalls.

multiple keys with the same name? what do you mean? arrays?

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

#4
post #2

Doesn't support multiple keys with the same name, which is valid and not particularly uncommon. I wouldn't be surprised if it breaks in other ways. This kind of thing is always full of subtle pitfalls.

You'd normally use this script when you were in full control of the parameters you are passing, so its fairly easy to avoid this situation.

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

#5
I was seeking to avoid the same thing recently. Instead of adding more JavaScript, I decided to use PHP. While we're using Rails, it's very inefficient to fire up the whole engine to do something as simple as outputting a script with a few interpolated variables. A simple PHP script can put out 2-3 times as many requests per second as Rails/Django for a task like that.

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

#7
post #3
post #2

Doesn't support multiple keys with the same name, which is valid and not particularly uncommon. I wouldn't be surprised if it breaks in other ways. This kind of thing is always full of subtle pitfalls.

multiple keys with the same name? what do you mean? arrays?

I think he means duplicate keys, as in '?id=4&color=8&id=6'

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

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

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

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

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

#10
post #3
post #2

Doesn't support multiple keys with the same name, which is valid and not particularly uncommon. I wouldn't be surprised if it breaks in other ways. This kind of thing is always full of subtle pitfalls.

multiple keys with the same name? what do you mean? arrays?

There's no such thing as parameter arrays. Any parameter name can have multiple values. PHP just happens to turn those into arrays. (I've never tried it in PHP with a parameter name that didn't have brackets at the end. It might not work in PHP.)

If you're familiar with Django, this is why the GET, POST and other request dictionaries are actually MultiValueDicts. If you call request.GET.getlist('varname') instead of request.GET['varname'], you'll get a list of each value passed with that name. The brackets aren't special, and if you used them, you'd need request.GET.getlist('varname[]').

Post reply on HN