0

This is a snippet of my `index.html

<div class="maincontainer">
    <h2 id="heading" class="heading"></h2>
    <input type="button" id="resetPassword" class="resetPassword" onclick="resetPassword()" value= "Reset Password" />
    <form method="post" onsubmit="return submitform()">
        <div>
            <input type="password" id="password" name="password" class="patternlock" />
            <input type="submit" value="login"/>
        </div>
    </form>
</div>

This is a snippet of my script.js which I included in my index.html

function submitform(){
var passwordvalue = document.getElementById("password").value;
var digits = getlength(passwordvalue);
if(digits<5) {
    raiseerror("lengthTooSmall");
}

checkduplicatedigits(passwordvalue);

if (errorraised == false && passwordset == false) {
    localStorage.setItem("passwordvalue", passwordvalue);
    successmessage("patternStored");
}
else if ( errorraised == false && passwordset == true) {
    if (localStorage.getItem("passwordvalue") == passwordvalue) {
        successmessage("screenUnlocked");
    }
    else {
        raiseerror("IncorrectPattern");
    }
}
return true;
};

function successmessage(successcode) {
    if(successcode == "screenUnlocked") {
        alert("You have unlocked the screen!");
        window.location = "./welcome.html";  //bug to be fixed
    }
    if (successcode == "patternStored") {
        alert("Your pattern is stored. Thanks.");
        passwordset = true; 
    }
    if (successcode == "resetSuccess") {
        alert("Pattern Reset Success!");
    }
    location.reload();
};

Here whenever, successcode == "screenUnlocked" is triggered, I am able to get the alert message

alert("You have unlocked the screen!");

but the next line

window.location = "./welcome.html";

is not working.

Why isn't it working in this case? Are there any specific requirements for window.location to work? If so, please specify.

Checked this code on Google Chrome 39.0.2171.71. I have checked my console for errors but there aren't any. There are only a few warnings but they are not related to this.

Or are there any other functions to make a redirection using pure Javascript? If so, please specify.

Please comment if any further information is required.

kvn
  • 2,210
  • 5
  • 20
  • 47

3 Answers3

1

First, this is not secure so I hope this is not for real protection.

The reason why it fails is you have a race condition with window.location and the form submission. The form is submitting because you are not cancelling the form submission.

You need to return false, not true at the end of the validation function.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Thanks for the information. I have to submit the form in all other cases except when `successmessage("screenUnlocked");` is invoked. So return false for the submitform() isn't working. – kvn Dec 03 '14 at 16:10
  • `return true` where you need it to submit. `return false` when you need it to fail. – epascarello Dec 03 '14 at 16:12
  • Yes, right. But I am really puzzled to see return false is not stopping the form from submitting. Which thereby is not letting the page to redirect. – kvn Dec 03 '14 at 16:28
  • `successmessage("xxx"); return false;` should work. If not maybe there is a JS error in the other code I am not seeing. Do you have log being persisted in your debugger? – epascarello Dec 03 '14 at 16:34
  • Thanks, my problem solved. I just had to move `window.location = "./welcome.html"` to `submitform()`. Changing location in a different function i.e successmessage("xxx") created the problem. – kvn Dec 04 '14 at 07:32
  • The working code is `successmessage("screenUnlocked"); window.location = "./welcome.html"; return false;` window.location should be written in the same submitform() function and not in successmessage("xxx"). Thanks for the help. – kvn Dec 04 '14 at 08:11
0

replace

successmessage("screenUnlocked");

by

successmessage("screenUnlocked");
return false;
M.S.
  • 442
  • 3
  • 13
  • But I have to admit that I do not get what your intention is, do you want to submit the form, or do you want the submission to be aborted and instead redirect to another page? and do you like to stay on the same page in the other cases? – M.S. Dec 03 '14 at 13:36
  • Yes, I want to submit the form in every case except when the `screenmessage("screenUnlocked")` is invoked. In all other cases, the form should be submitted. – kvn Dec 03 '14 at 16:07
  • `successmessage("screenUnlocked"); return false;` is not working. I'm really puzzled to see this. – kvn Dec 03 '14 at 16:09
-1

I think you need to use

window.location.href = "./welcome.html";

The issue is best answered here

How to redirect to another webpage in JavaScript/jQuery?

Community
  • 1
  • 1
strapro
  • 153
  • 8
  • While this may be technically correct, the `window.location` should work and does in Chrome. The issue has to do with the form submission, I suspect. – rfornal Dec 03 '14 at 13:33