Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts

    jQuery.url - easy URL parameter retrieval

    This is a simple jQuery plugin to pull parameters from a URL; if not available, will return 'undefined'

    For example, if the page is http://example.com/index.html?foo=bar, $.url("foo"); will return bar

    Code:
    jQuery.url = function(paramName) {
    	var results = new RegExp("[\\?&]"+paramName.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]")+"=([^&#]*)").exec( window.location.href );
    	if(results == null){return "undefined";} else{return results[1];}
    };
    Not sure how useful this really is, but I hope it helps somebody.

  • #2
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,459
    Thanks
    1
    Thanked 23 Times in 21 Posts
    Sounds interesting. A couple observations:

    • I learned a while ago that else after return is not needed.
    • Your check for results == null is also a little off. You could simply write !results .
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •