2

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 = "";
        }
    }
Domino
  • 6,314
  • 1
  • 32
  • 58
Hayden E
  • 108
  • 8

3 Answers3

1

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

Domino
  • 6,314
  • 1
  • 32
  • 58
  • Since " window.open("advance.html"); " works to open a new tab I don't think there's an error elsewhere. So is there a different way to open a page in the same tab? Because none of these work. – Hayden E Apr 13 '17 at 15:20
  • @HaydenE The `window.location` assignment should work. Are there any errors in the console? – Barmar Apr 13 '17 at 15:30
  • Yes, this one: "10:37:22.303 The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol. 1 index.html" – Hayden E Apr 13 '17 at 15:38
  • @Barmar I added to the HTML documents of both pages and now I have no console errors. Still not opening the second page though. – Hayden E Apr 13 '17 at 15:43
  • That's just a warning, it shouldn't affect whether the page is displayed. If changing something in the header of the page removes the warning, it obviously means that it's loading the page. – Barmar Apr 13 '17 at 15:44
0

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");
   }
}
Styx
  • 9,863
  • 8
  • 43
  • 53
mmm kkk
  • 1
  • 1
0

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.

Abdullah Al Mubin
  • 123
  • 1
  • 2
  • 10