WTF!!! JQuery provides no way to access URL parameters, seriously?!@!!

So I haven’t fully partaken of the jquery kool-aid just yet. And just now I am annoyed enough to write this quicky rant before I get back to coding.

So I have a situation where I need to pass a url (GET) param to a page so that javascript can access it.

My first thought was:

In the past I’ve written little functions to access GET vars from javascript, but I’d have to go digging around to find it. Or I could google for one, but frankly it annoys me to put an extra 5 or so lines in the file just for doing something so simple and routine. Something that PHP makes SO easy.

PHP way:

echo @$_GET['my_url_var'];

Javascript way:

// Roll your own parsing function. This is one of many variants that google turned up.$.urlParam = function(name){ var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href); return results[1] || 0;} alert( $.urlParam('my_url_var') ); 

My second thought was:

Since javascript is such a PITA with accessing get params, I’ll just grab it with PHP, put it in a smarty variable inside a <script> tag, and the JS can then access it. Easy enough. But kind of gross, especially as I was considering using a global variable.

My third thought was:

Wait, this app is already using jquery and jquery does all sorts of magical things and causes a bajillion JS files to get downloaded and generally is supposed to make life easier than coding to the bare JS engine, so it must provide a simple built-in way to access GET variables since these are after all the primary programmatic input to any web page and all jquery apps are after-all web pages… no?

It turns out NO. At least according to Google. Searching “jquery url parameter” and “jquery url var” returns pages of posts by other people running into the same problem and posting the same tired solutions using Window.location.href.parse, or uglier yet regex.

Serious jQuery FAIL.

yes, there is at least one jQuery plugin that somebody posted to do this. But for such a simple thing, I’m in no mood to go download someone’s random plugin, add to source control, add to all my html pages, etc, for every project I work on. This should be a built-in, period.

END RANT. Back to work.

If it is a built-in and my googling skills are deficient, someone please let me know asap!

Facebook Comments

comments

Tagged with: , , ,
Posted in post
2 comments on “WTF!!! JQuery provides no way to access URL parameters, seriously?!@!!
  1. Hans Doller says:

    Heh, Solution: Don’t use JQuery.

    // get query string vars
    var obj = Ext.Object.fromQueryString(‘foo=bar&hello=world’);

    // recreate query string
    var urlGet = Ext.Object.toQueryString( obj );

    // assemble final url
    var url = Ext.urlAppend( ‘http://somesite.com/’, urlGet );

    • danda says:

      I’m not sure I understand the example.

      All I want is the equivalent of $_GET['some_url_var'] in PHP. So using your example, it might look like:

      var some_url_var = Ext.urlGet( ‘some_url_var’];

      So does something like that exist? Or does Ext fail also? :D

Leave a Reply