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?

6
  • 1
    Please check this one stackoverflow.com/questions/4656843/… Commented Feb 13, 2014 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? Commented Feb 13, 2014 at 12:14
  • You are mixing some some ASP JS code here: check this: stackoverflow.com/questions/12049620/… Commented Feb 13, 2014 at 12:14
  • or try this something like this: <%=Request.QueryString("myvariable") %> Commented Feb 13, 2014 at 12:15
  • Have a look here : jquery4u.com/snippets/url-parameters-jquery Commented Feb 13, 2014 at 12:15

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.