Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

get url parameter value in javascript

Posted on 2013-11-18
12
390 Views
Last Modified: 2013-12-17
HI,

I have a URL which has the parameter "ID"

how can i get the value of the ID in javascript

please help

Thanks

R8VI
0
Comment
Question by:R8VI
  • 5
  • 3
  • 2
12 Comments
 
LVL 33

Accepted Solution

by:
Big Monty earned 500 total points
ID: 39657705
try this:

var QueryString = function () {
  // This function is anonymous, is executed immediately and 
  // the return value is assigned to QueryString!
  var query_string = {};
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    	// If first entry with this name
    if (typeof query_string[pair[0]] === "undefined") {
      query_string[pair[0]] = pair[1];
    	// If second entry with this name
    } else if (typeof query_string[pair[0]] === "string") {
      var arr = [ query_string[pair[0]], pair[1] ];
      query_string[pair[0]] = arr;
    	// If third or later entry with this name
    } else {
      query_string[pair[0]].push(pair[1]);
    }
  } 
    return query_string;
} ();

Open in new window

You can then access QueryString.id
0
 

Author Comment

by:R8VI
ID: 39657796
Hi,

Thanks for this but where do I substitute the ID in your code

R8vi
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39657803
wherever you're looking to use the ID value, such like this:

   var idParam = QueryString.id;

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:R8VI
ID: 39659169
I am sorry could you please explain a bit further
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39659184
Ultimately what are you trying to accomplish with the url perameter?
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39659219
Try:
var id = document.getElementById("URL").getAttribute("id");
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39659390
doesn't work in Chrome...
0
 

Author Comment

by:R8VI
ID: 39659658
Hi,

So I have a URL like so

http://localhost:49768/Solution/IframePage.aspx?Name=Ellis&URL=http://www.test.com

I would like to get the Value of "URL" which in this case would be http://www.test.com
 and store it in a variable

Thanks

R8VI
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39659669
ok, so using my example above, just create a variable:

var myURL;

then assign it like:

myURL = QueryString.URL;

the function I gave earlier runs upon page load, and it basically creates an "object" with properties that are the url parameters
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39662273
You could also toggle the string with JS string functions (see http://w3schools.com/js/js_obj_string.asp) and extract the URL.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question