0

I have the code

window.open("http://localhost/xyz.php")

This opens the PHP file on a new tab, although the same window. I want to open this on the same tab. How do I do it.

I have tried window.open("http://localhost/xyz.php","_self") and window.open("http://localhost/xyz.php";"_self") but that does not work. I have also tried window.location.hrefbut nothing seems to work. Is there any other way?

Ilmo Euro
  • 4,925
  • 1
  • 27
  • 29
Sachin S
  • 33
  • 5

4 Answers4

1

Just use window.location object:

window.location.href = 'http://localhost/xyz.php';

Changing href property, changes the current page address to the one you give as a parameter...

Note: you could also use window.location = 'http://localhost/xyz.php';, as suggested in comment, which is a bit shorter; though, changing location.href is a bit more standard, being implemented since JavaScript 1.0, so it's available in all browsers...

MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • This does not work. I have tried this already. the problem when I use this, is that it does not open the PHP page at all. – Sachin S Apr 20 '16 at 16:20
1

try this

window.location.replace("http://localhost/xyz.php");
JYoThI
  • 11,977
  • 1
  • 11
  • 26
  • I didn't know (the specifics of) this technique. Worth noting that, [according to MDN](https://developer.mozilla.org/en-US/docs/Web/API/Location/replace), this will have special effect on the browser history. – Jeroen Apr 20 '16 at 09:48
  • @vinayakj Could you elaborate on how this does not attempt to answer the question? – Jeroen Apr 20 '16 at 19:00
  • @Jeroen this is very unsecure way to open something in same tab. – vinayakj Apr 22 '16 at 16:32
-1

Please refer to Open a URL in a new tab (and not a new window) using JavaScript .

It is a decision of the user, not the programmer, if a new page is opened in a new tab or a new window.

Community
  • 1
  • 1
TvR
  • 61
  • 3
  • 1
    OP wants to open the new page in the **same** tab, so I suppose she just wants to change location... – MarcoS Apr 20 '16 at 09:26
-1

Try use this in your js:

location.href = "http://localhost/xyz.php";

Grey13
  • 58
  • 7