Solved

get url parameter value in javascript

Posted on 2013-11-18
12
382 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 32

Accepted Solution

by:
Big Monty earned 500 total points
Comment Utility
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
Comment Utility
Hi,

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

R8vi
0
 
LVL 32

Expert Comment

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

   var idParam = QueryString.id;

Open in new window

0
 

Author Comment

by:R8VI
Comment Utility
I am sorry could you please explain a bit further
0
 
LVL 32

Expert Comment

by:Big Monty
Comment Utility
Ultimately what are you trying to accomplish with the url perameter?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 9

Expert Comment

by:Sar1973
Comment Utility
Try:
var id = document.getElementById("URL").getAttribute("id");
0
 
LVL 32

Expert Comment

by:Big Monty
Comment Utility
doesn't work in Chrome...
0
 

Author Comment

by:R8VI
Comment Utility
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 32

Expert Comment

by:Big Monty
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now