latest tweet: Twice in a two months I have managed to pay 3 times for my hosting package. What an idiot.— 11 hour ago

jQuery URL Parser v1.0

This jQuery URL Parser is a little plugin that I wrote a while back and have now decided to release properly. It’s based on Steven Levithian’s regex-based URI parser, and allows you to quickly and painlessly access pretty much any bit of information within the URL.

Why would you need to pull information out of the URL? Well there are lots of potential reasons. I have been using it on a CodeIgniter-based CMS project recently where each page initially loads the same one JS file, which in turn selectively loads others depending on the URL (roughly mirroring the way CodeIgniter loads different controllers by parsing the URL segments). I also often use it just to get the base URL of the site so that I can use absolute URL references in my JavaScript, without having to change anything when the site moves domains - say from the testing to the production server. It also offers an easy way to access query string parameters for use in your scripts, and much more.

You can download the plugin from the project page, together with the documentation etc.

I’ll post up any updates to the plugin here, so grab the RSS feed if you want to stay updated. For bug reports or feature requests please visit the jQuery plugins page at http://plugins.jquery.com/project/url_parser, or if you have any other questions or queries, please drop me an email at mark[at]allmarkedup[dot]com and I will do my best to get back to you as soon as I can - but I’m pretty busy at the moment so please be patient!

12 Responses to “jQuery URL Parser v1.0”

  1. Adrian,

    I think the documentation at http://projects.allmarkedup.com/jquery_url_parser/ is wrong in that jQuery.URL should be jQuery.url

  2. James,

    Thanks for this, im sure it will be useful.

    A quick addition that would be good: if you could also target a link element using a jquery selecotr and then just pull the details of the href straight out of the targeted element.

    Cheers

  3. Rob,

    In the same vein, I’ve coded up a JavaScript URI object based on Steven’s parser.

    You can try out an interactive example here: http://appengine.bravo9.com/b9j/example/uri/

  4. Mark,

    Thanks @Adrian - documentation has been amended.

    @Rob - nice! Good work, looks very comprehensive.

  5. Jacob,

    Mark — along with the online documentation, you might also want to fix the readme.htm in your zip file.

  6. Mark,

    Thanks @jacob, missed that one! Should all be updated now.

  7. Nek.D,

    There’s a little error in your doc (online & offline)
    “jQuery.url.setURL(”http://allmarkedup.com/category/javascript/#footer”).attr(”anchor”) // returns ‘footer’”
    should be setUrl

    Anyway, i love this plugin, thanks

  8. josh,

    Please forgive me if I am way off track.

    Is it possible to display a different html page based on the inbound url a user comes from to access my site. Say if I wanted people coming from “x” site to see a certain block of conent but I don’t want people coming from “y” site to see it but something special for them?

    Make sense?

    Any ideas are much appreciated. Trying to learn more about this possibility.

  9. Mark,

    @Nek.D - thanks for the heads up, I have amended the docs now. Glad you like it!

    @josh - you could definitely do this, but you probably woudln’t want to do it using javascript. The best way to do it would be to use a server-side language to check the referer - in PHP you could do this using $_SERVER["HTTP_REFERER"] to see where the use had come from.

    But be warned - it is pretty easy to spoof a referer string so don’t use it for anything that that needs to be secure!

    Hope that helps.

  10. josh,

    thanks for the feedback. I am not sure I follow you with the PHP suggestion. I am not as familiar. If you have any suggestions where to find more about what you mentioned please let me know.

  11. Mark Drew,

    Just curious here, but can I pass “ANY” url string and parse that? how would you do that? I can’t see it in the docs (rather than just passing the current url) ?

  12. Mark,

    @Mark Drew - You can use any url string by chaining in the setUrl() method, so if you wanted to parse the url “http://mysite.com/different_url.php?test=4″ to get the value of ‘test’ from the query string you would do:

    jQuery.url.setUrl(”http://mysite.com/different_url.php?test=4″).param(”test”)

    I hope that makes sense! Let me know if anything is not clear.

Leave a Reply