Solved

get url parameter value in javascript

Posted on 2013-11-18
12
389 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

785 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