2

I am sending one variable through an html file to another but using the code bellow to the second file it does't grab the variable.

For example : From i am sending myfile.html?myvariable=x

and i am trying to grab it with the code bellow..

<script type="text/javascript">
$(document).ready(function() {
var myletter = Request.QueryString("myvariable");
alert (myletter);
});
</script>

Why it's not working?

Bhavesh G
  • 3,000
  • 4
  • 39
  • 66
Irene T.
  • 1,393
  • 2
  • 20
  • 40
  • 1
    Please check this one http://stackoverflow.com/questions/4656843/jquery-get-querystring-from-url – Pavlo Feb 13 '14 at 12:13
  • What do you mean "doesn't grab the value". Is it bringing up the alert at all? Is there an error message? – Mister Epic Feb 13 '14 at 12:14
  • You are mixing some some ASP JS code here: check this: http://stackoverflow.com/questions/12049620/how-to-get-get-variables-value-in-javascript – reyaner Feb 13 '14 at 12:14
  • or try this something like this: `<%=Request.QueryString("myvariable") %>` – reyaner Feb 13 '14 at 12:15
  • Have a look here : http://www.jquery4u.com/snippets/url-parameters-jquery/ – phron Feb 13 '14 at 12:15
  • Alert doesn't works.. I don't get anything @ChrisHardie i want to retrieve the x value on the second page – Irene T. Feb 13 '14 at 12:16

3 Answers3

2

Wanna to do it in JS:

<script type="text/javascript">
var match = (window || this).location.href.match(/\?(.*)$/);;
match = match ? match[1] : '';
alert(match.split("=")[1]);
</script>

njoy

Mahavir
  • 322
  • 4
  • 8
0

Did you try this?

<script type="text/javascript">
$(document).ready(function() {
    var myletter = '<%=Request.QueryString("myvariable"); %>';
    alert (myletter);
});
</script>

With jQuery, you could do it like this:

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

And then:

$(document).ready(function() {
    var myletter = $.getUrlVar('myvariable');
    alert (myletter);
});
reyaner
  • 2,799
  • 1
  • 12
  • 17
0

and for every single other possible way of doing this in javascript: How can I get query string values in JavaScript?

Community
  • 1
  • 1
Jan Drewniak
  • 1,316
  • 15
  • 18