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??
jollykhAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.