[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 580
  • Last Modified:

Get Parameter Value from URL

Hi Experts,

I need to get the value of a parameter in the URL.
ex:
http://localhost/test/testpage.jsp?clear=yes&test=false

I simply want the value of "clear".  How can I get this in Javascript??
0
jollykh
Asked:
jollykh
1 Solution
 
BogoJokerCommented:
Hi jollykh,

It might sound bad but here was the first link off of a google search:
http://www.activsoftware.com/code_samples/code.cfm/CodeID/59/JavaScript/Get_Query_String_variables_in_JavaScript

Code:
 <script>
function getQueryVariable(variable) {
  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 (pair[0] == variable) {
      return pair[1];
    }
  }
  alert('Query Variable ' + variable + ' not found');
}
</script>

You would do this to get clear

<script>
  alert( getQueryVariable("clear") );
</script>

Joe P
0
 
jollykhAuthor Commented:
I saw that but that code doesn't work.  I tested it out and I didn't get back my value.  I just recieved an alert box that said Query Variable clear not found.
0
 
NicksonKohCommented:
Hi BogoJoker,

try this.

<html>
<head>
<Script>
function GetParamValue(param)    {
    var url = document.URL;
    var val = url.replace(eval("/.+\?.*"+param+"=([^&]*)/"), "$1");
    alert(val);
}
</Script>
</head>
<body>
<form name="form1" method="post" action="test2.asp">
  <table width="100%"  border="0">
    <tr>
      <td><input type="button" name="Button" value="GetParameter" onClick="GetParamValue('clear')"></td>
    </tr>
  </table>
</form>
</body>
</html>


Cheers
Nickson
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
thirdCommented:
hi jollykh,

BogoJoker's code should work. can you show us your code?
0
 
ZvonkoSystems architectCommented:
My method is this:

<script>
function getParam(theKey){
  var qV = location.search.split(RegExp("[?&]"+theKey+"="));
  if(qV.length>1){
    return unescape((qV[1]+"&").split("&")[0]);
  }
}

var clearVal = getParam("clear");
alert(clearVal);
</script>

0
 
jollykhAuthor Commented:
I need to apologize to everyone.  I failed to mention that this is happening within a frame.  Everyone's code that I tried worked so I'm going to split the points.  Hope you are ok with this.
0
 
BogoJokerCommented:
You wanted to split the points but it looks like that wasnt the result =)
You can reopen the question by posting in Community Support, make sure you tell them this Question is: Q_21827485
Split Points is a link down at the bottom by the comment text box.  Hope everyone gets their share of the points if you indeed wanted to split the points.

Joe P
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now