« Previous entry | Next entry » Browse > Snippets

Skip to comments (11) get a GET parameter in javascript
Posted by Erik on Jan 15 2006 @ 19:12  :: 9111 unique visits

Just one simple function:
CODE: JAVASCRIPT
function getget(name) {
  var q = document.location.search;
  var i = q.indexOf(name + '=');

  if (i == -1) {
    return false;
  }

  var r = q.substr(i + name.length + 1, q.length - i - name.length - 1);

  i = r.indexOf('&');

  if (i != -1) {
    r = r.substr(0, i);
  }

  return r.replace(/\+/g, ' ');
}


Example usage: in index.html
CODE: JAVASCRIPT
alert('Hello: '+getget('name'));

If you open this index.html with index.html?name=Erik it will alert "Hello: Erik".

11 comments posted so far
Add your own »

1. On Jun 20 2006 @ 00:51 guest wrote:

nada

2. On Jul 21 2008 @ 02:45 guest wrote:

good

3. On Aug 18 2008 @ 08:28 guest wrote:

WOW! It actually works!

4. On Apr 15 2009 @ 13:03 guest wrote:

thx,
it works very smoothly and is an elegant solution

5. On Apr 15 2009 @ 23:32 YanickD wrote:

thx!

6. On Apr 18 2009 @ 11:55 Ponting wrote:

Hi, I am a new comer in web development and web programming field. At present, I am working on parameters in java script after my certification exams VCP-310 for VMware Certified Professional on VI3.5 and 640-802 Exam. These there is a gap between my exams so that’s why I am working on java script. My next exams are going to start on next month including 350-001 Exam for becoming CCIE Cisco Certified Internetworking Expert and 70-290 Exam for Managing and Maintaining a Microsoft Windows Server 2003 Environment. After these two exams there will only one exam remaining that exam is Microsoft 70-640 Exam going to be held in the end. I must say that you did a nice job by writing the handy code. Keep carry on the handy work man.

7. On May 01 2009 @ 10:16 LukeDeverock wrote:

Thank you! Extremely elegant and works just as required.

8. On May 04 2009 @ 19:50 guest wrote:

If the request string has DoNotUpdate=true this method would match this pattern when called with Update, the script correctly finds the equals sign at the end of the parameter, but does not check that the request parameter starts with a ? or &.

9. On May 13 2009 @ 15:50 guest wrote:

Need Furniture? And need to buy furniture from China at competitive price? LongYear Furniture is your source for quality bedroom furniture featuring a huge selection of home furniture a happy home for beautiful life, kids furniture for a good memory of childhood, and to gain an extra good price from wholesale furniture and direct from  furniture manufacturers China, styles of China furniture are available, living room furniture is also nice for your house, find dining room furniture and more!

11. On Jan 05 2010 @ 14:26 uggbaileybutton wrote:

bailey button uggs

-ugg boots cheap

ugg boots uk

ugg classic

Add a new comment

Name:
Password: (leave empty for anonymous comment)
 
View formatting tags Comment: