Hayden E
Hayden E

Reputation: 108

Open new HTML page in the same tab via Javascript

I have tried a half dozen ways to open a page in the same tab upon clicking a button and none of them work on FireFox or Edge:

window.location ="advance.html";

nothing happens

window.location ="/advance.html";

nothing happens

window.location.href ="advance.html";

nothing happens

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

nothing happens

window.open("advance.html", _self);

nothing happens

window.open("advance.html");

opens page in new tab

Been working on it for an hour trying every suggestion and nothing works.

What am I missing here?

EDIT: Since people are asking for the context (I know the username and password are in plain text, I'm just trying to get this part working first):

<script language="javascript">
    function goHome()
    {
        window.location = 'index.html';
    };

    function login(form)
    {
        if(form.company.value == "advance" && form.password.value == "1234")
        {
            window.location.href ="advance.html";
        }
        else if(form.company.value == "" || form.password.value == "")
        {
        }
        else
        {
            alert("Incorrect Usename/Password combination please try again.");
            form.password.value = "";
        }
    }

Upvotes: 2

Views: 2801

Answers (3)

Abdullah Al Mubin
Abdullah Al Mubin

Reputation: 133

I think the problem is in your window.open("advance.html", _self);. You have to quote the "_self". window.open("advance.html", "_self"); this code should work. Also you can use location parameter in it. Like `location.assign("advance.html"). It will open it in a new window.

Upvotes: 0

mmm kkk
mmm kkk

Reputation: 1

Do not use submit, use the button like this:

<input type="button" onclick="login(this.form)" value="login">

function login(form) {
   if (form.company.value == "advance" && form.password.value == "1234")
   {
      window.open("advance.html", "_top");
   }
}

Upvotes: 0

Domino
Domino

Reputation: 6778

I've actually figured this out by experimenting because the MDN documentation wasn't clear on this, but it seems window.location uses full paths with protocol, but accepts relative paths that begin with . or .. and absolute site paths that begin with /.

This means your second and fourth examples actually work.

window.location = "/advance.html";

If that's not working for you, then the error might be elsewhere, or browsers are inconsistent (which would surprise me since this is an old feature).

Upvotes: 1

Related Questions