0

Suppose that I have a URL like the following:

http://localhost:8000/intranet/users/view?user_id=8823

Now, all I want to do is to get the value of the URL using JavaScript and parse it, taking the user_id value (which is 8823 in this case) and sending that value through an iframe.

How can I do this?

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
Johnny
  • 1,555
  • 3
  • 14
  • 23

4 Answers4

1

try this code

function getParameterByName(name)
{ 
      name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
      var regexS = "[\\?&]" + name + "=([^&#]*)"; 
      var regex = new RegExp(regexS); 
      var results = regex.exec(window.location.href); 

   if(results == null) 
      return ""; 
   else 
   return decodeURIComponent(results[1].replace(/\+/g, " ")); 
} 

i found it at How can I get query string values in JavaScript?

Community
  • 1
  • 1
Kashif Khan
  • 2,615
  • 14
  • 14
0

Try using window.location.href or document.URL

Max
  • 8,671
  • 4
  • 33
  • 46
0

Do this:

var matches = document.location.search.match( /user_id=(\d+)/ );
if ( matches != null )
{
    alert( matches[ 1 ] );
}

matches[ 1 ] will contain the user ID. document.location.search contains the query string (all of the parameters which follow the '?' including the '?').

Yaniro
  • 1,595
  • 9
  • 14
0
var test = "http://localhost:8000/intranet/users/view?user_id=8823";
//var url = document.URL;
var url = test.split("=");
var urlID = url[url.length-1];

document.write(urlID);

window.frames["myIframe"].yourMethod(urlID);
Tim Vermaelen
  • 6,869
  • 1
  • 25
  • 39