Js script crashing entire element

142 views Asked by At

I am making a site on google sites. I have a button made by code that detects if you put a certain value into an input box, it should then load another site. But when I enter that correct value, the object crashes.

I've tried a lot of different things, but nothing works.

<form name="form1" method="GET">
    <input name="codebox1" type="text" />
    <input name="button1" type="submit" value="Check Code" onClick="return testResults(this.form)"/>
</form>

<script>
function testResults (form) {
    if (form.codebox1.value == "Doovhhlqjhbh") {
        window.location = 'https://sites.google.com/view/the-mbd-project/There-are-secrets-to-be-discovered';
        window.alert("Correct Code. Connecting...");
    }
    else {
        window.alert("Wrong Code, try again!");
    }  
    return false;
};
</script>
1

There are 1 answers

2
Mahdi Najafi On

window.location = string; is wrong . You should use this to change the browser address. Also, alert must be executed before switching the page

change your function to this:

function testResults(form) {
    if (form.codebox1.value == "Doovhhlqjhbh") {
        window.alert("Correct Code. Connecting...");
        setTimeout(() => {
            window.location.href = 'https://sites.google.com/view/the-mbd-project/There-are-secrets-to-be-discovered';
        }, 0);
    }
    else { window.alert("Wrong Code, try again!"); } 
    return false;
}