views:

77

answers:

3

This code is what I use now. But it does not work when I try to use an array to compare values. If anybody has any idea of why, please respond.

<html>
 <head>
  <script type-'text/javascript'>

   function hovedFunksjon()
    {
     //alert("test av funksjon fungerte");
     //alert(passordLager);
     window.open("index10.html","Window1","menubar=no,width=430,height=360,toolbar=no");
    }

   function inArray(array, value) {
    for (var i = 0; i < array.length; i++) {
     if (array[i] == value) return true;
      }
       return false;
      }

   function spørOmPassord()
    {
     var passordLager = ["pass0","pass1","pass2"];

     window.passordInput = prompt("password");//Ved å bruke "window." skaper man en global variabel

     //if (passordInput == passordLager[0] || passordLager[1] || passordLager[2])
     if (inArray(passordLager,passorInput) )

      {
       hovedFunksjon();
      }
     else
      {
       alert("Feil passord");
       //href="javascript:self.close()">close window
      }
    }
   function changeBackgroundColor()
    {
     //document.bgColor="#CC9900";
     //document.bgColor="YELLOW"
     document.bgColor="BLACK"
    }
  </script>
 </head>

 <body>
  <script type-'text/javascript'>
   changeBackgroundColor(); 
  </script>
   <div align="center">
    <form>
     <input type = "button" value = "Logg inn" onclick="spørOmPassord()">
    </form>
   </div>
 </body>
</html>
A: 

You assign to window.passordInput but pass passordInput - they are not the same.

If you open this in FireFox you can see error messages generated by the execution of your script via Tools->Error Console.

Alex K.
Actually, properties of the `window` are accessible as global variables, so assigning `window.passordInput` creates the global `passordInput`. As it happens, there's a typo in his code and he actually passes `passorInput`, so your answer could have a little validity if you mistyped yourself ;-)
Andy E
Actually, in this case `window.passordInput` and `passordInput` are the same as there is no `passordInput` in the current scope.
Gumbo
+3  A: 
 if (array[i] == value) return true;
  }
   return false;
  }

That's really misleading indentation there!

window.passordInput = prompt("password");

I'm not sure why you're using a global to store the input, since you're only using it in the local function. (If you really needed a global, you don't need the window. prefix since you haven't declared that variable with a local var anyway.)

This may be your problem though: prompt is no longer usable in IE7+. Microsoft have stopped it working, for (extremely dubious) security reasons. You'll probably need to come up with another method involving a form field, eg.:

<input id="password" type="password"/>
<input id="login" type="button" value="Login"/>

<script type="text/javascript">
    document.getElementById('login').onclick= function() {
        var password= document.getElementById('password').value;
        if (['pass0', 'pass1', 'pass2'].indexOf(password)!==-1) {
            window.open('thing.html', '_blank');
        } else {
            alert('no.');
        }
    };
</script>

I'm using Array#indexOf here to do the in-list test. This is a standard method in the new version of JavaScript, but not every browser supports it yet. You can add it to browsers that don't, like this:

// Add ECMA262-5 Array indexOf if not supported natively
//
if (!('indexOf' in Array.prototype)) {
    Array.prototype.indexOf= function(find, from /*opt*/) {
        for (var i= from || 0, n= this.length; i<n; i++)
            if (i in this && this[i]===find)
                return i;
        return -1;
    };
}

Either way, be aware that JavaScript password “protection” is not only awful for accessibility but also totally insecure. You should never use it on anything you care about at all. Look up real HTTP Basic Authentication (htaccess) and/or cookie-based form logins if you want to do it properly.

bobince
+1. I don't think IE7 blocks `prompt` completely. IE7+ just (ironically) prompts you to allow or deny scripted windows. Even still, `prompt` is very archaic and modern JS dialog replacements are much better.
Andy E
Yeah, it's still there, but the hurdles you have to jump through to activate it are enough to make it unusable in practice, especially given the way IE silently messes up the script on the first call.
bobince
+1  A: 

You have just forgot a d in passorInput:

inArray(passordLager,passordInput)
Gumbo
+1 - I think this is the actual problem here.
Andy E
that was it thanks :)
Java starter