0

So you know how sometimes websites have a little "?variable=value" thing at the end of their name? how do I use this? I'm pretty sure either A) the variable isn't getting deffined like it should or B) there is a problem regarding this portion of the code:

if (! variable) {
  var variable;
}

So how do I do this?

EDIT: Here is the full script I'm trying to run, it's purpose is for a simple password protected page, but I want to allow the password to be passed in the url so when switching pages you won't have to re-enter the password continuously.

  <script>
      var m = location.href.match(/[?&]variable=([^&]*)/),
      password;
      if (m) {
        password = m[1];
      }
      var pass1="password";
      var pass2="abcd";
      if (password!==pass1 || password!==pass2) {
        password=prompt('Please enter your password to view this page!',' ');
        if (password==pass1 || password==pass2) {
          alert('Password Correct!');
        } else {
          history.go(-1);
        }
      }
  </script>

This code just prompts me no matter what is in the url.

BBMAN225
  • 653
  • 4
  • 10
  • 17
  • 2
    Here you go: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values/901144#901144 – Tmdean Mar 07 '13 at 04:18
  • Why are you using `if (! m) {`? It's `if (m) {`! – Ja͢ck Mar 07 '13 at 05:18
  • Oops! Okay, I changed it and now it just prompts me for the password. – BBMAN225 Mar 07 '13 at 05:19
  • @Jack any ideas? I'm stumped. Also, password is undefined and I think this is the problem. – BBMAN225 Mar 07 '13 at 05:37
  • Well, what does the location in the address bar look like? i.e. what's `location.href`? – Ja͢ck Mar 07 '13 at 06:06
  • It looks like the web address, exactly like it. Something along the lines of `http://www.website.com/page?password=abcd` or at least, that's what came up when I "alert(location.href);" – BBMAN225 Mar 07 '13 at 07:08
  • Well, I think I figured it out.. this `var m = location.href.match(/[?&]variable=([^&]*)/),` needed to be `var m = location.href.match(/[?&]password=([^&]*)/),` of course now I just gotta tweak the code a bit... – BBMAN225 Mar 07 '13 at 07:14

2 Answers2

7

Parameters in the query string are not automatically passed to Javascript; they're usually meant for server-side scripts. To get one variable you could do this:

var m = location.href.match(/[?&]variable=([^&]*)/),
password;

if (m) {
    password = m[1];
}
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • @BBMAN225 That's not very helpful. What did you try, why didn't it work? – Ja͢ck Mar 07 '13 at 04:44
  • Sorry, I tried this: var password = location.href.match(/[?&]variable=([^&]*)/); if (! password) { var password; } – BBMAN225 Mar 07 '13 at 04:47
  • @BBMAN225 I'm not even sure what that code is supposed to do, but fwiw I've updated my answer. – Ja͢ck Mar 07 '13 at 04:51
  • Well, I put your updated code in but it still seems to mess up. I tried to put this in the if (m) sequence but it didn't alert: "alert(password);" – BBMAN225 Mar 07 '13 at 05:11
  • @BBMAN225 Perhaps at this point it's better to update your question with the code as you have it now. – Ja͢ck Mar 07 '13 at 05:12
  • Okay done, please note that it is probably terrible and basic because I've never tried much more then a little variable handling here and there. Of course, I've done tutorials. – BBMAN225 Mar 07 '13 at 05:18
  • 4
    @BBMAN225 y u never copy/paste my code properly? – Ja͢ck Mar 07 '13 at 05:20
  • Sorry :P I fixed it now. – BBMAN225 Mar 07 '13 at 05:24
1

you can pass variable like this:

<script language="javascript" type="text/javascript">
var scrt_var = 10; 
</script>
<a href="2.html" onclick="location.href=this.href+'?key='+scrt_var;return false;">Link</a>

or like this:

put id attribute on anchor element

 <a id="link2">

set href attribute on page load event:

var scrt_var = 10;
var strLink = "2.html&Key=" + scrt_var;
document.getElementById("link2").setAttribute("href",strLink);
Amrendra
  • 2,019
  • 2
  • 18
  • 36