javascriptphphttp-redirecthtml-meta

window.location (JS) vs header() (PHP) for redirection


using JS : (in <head> tag)

<script>window.location="https://stackoverflow.com";</script>

using PHP : (in <head> tag)

header('Location: https://stackoverflow.com');
end();

Which one I should use ? or another ?

and what about using <meta>?

<meta http-equiv="refresh" content="0;url=https://stackoverflow.com"/> 

Solution

  • The result is same for all options. Redirect.

    <meta> in HTML:

    window.location in JS:

    header('Location:') in PHP: