1

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>
4
  • I don't understand what you mean. When you use an array where exactly to compare what? Commented Apr 11, 2010 at 14:41
  • why are all your function/var names 1 letter off real words? makes it really hard to understand your code. Commented Apr 11, 2010 at 14:45
  • 1
    You have a typo in your code, which may or may not be the cause of your problem: inArray(passordLager,passorInput) -- looks like it should be passordInput. Commented Apr 11, 2010 at 14:47
  • @Samuel... it's not English; but they are real words. Commented Aug 28, 2012 at 3:49

3 Answers 3

3
 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.

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

2 Comments

+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.
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.
1

You have just forgot a d in passorInput:

inArray(passordLager,passordInput)

Comments

0

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.

2 Comments

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 ;-)
Actually, in this case window.passordInput and passordInput are the same as there is no passordInput in the current scope.

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.