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
  • 2,972
  • 4
  • 37
  • 65
Irene T.
  • 1,353
  • 2
  • 18
  • 38

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
  • 7
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,779
  • 1
  • 10
  • 15
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,286
  • 14
  • 17