Javascript - Open new tab and close current

Asked

Viewed 939 times

1

How can I open a new guide on JavaScript and close the current?

I tried using this code but error 500 on the server

redirect.php

echo "</script>window.open(https://google.com, "_blank");</script>";
echo "</script>window.close();</script>";

Could someone help me ?

  • 1

    This is not PHP, it’s Javascript.

  • True, it was only a mistake

  • New tabs should only be opened with the user’s click. This kind of practice of opening new tabs without user interaction is not advisable and most browsers understand how to pop-ups and block.

  • And you need to escape by using double quotes on the content inside echo: echo "</script>window.open(\"https://google.com\", \"_blank\");</script>";

2 answers

1

Note that quotes are missing from your open and its opening tag of script already starts wrong.

echo "<script>window.open('https://google.com', '_blank');</script>";
echo "<script>window.close();</script>";

You can add the two between a single tag of script

echo "<script>window.open('https://google.com', '_blank');window.close();</script>";

1

Use single quotes (') or escape character (\) inside the string. Follow code:

echo "</script>window.open(https://google.com, '_blank');</script>";
echo "</script>window.close();</script>";

OR

echo "</script>window.open(https://google.com, \"_blank\");</script>";
echo "</script>window.close();</script>";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.