Agel Salazar Agel Salazar - 2 months ago 17
Javascript Question

Javascript: header is not defined

I already tried the 2 re-directions in JavaScript, the

window.location.href()
and
header()
but always show me an error:


ReferenceError: header is not defined


My code from PHP:

echo "<script> header('location: index.php');</script>";

Answer

There's no header() function in JavaScript. header() is a PHP function. Here is one way to redirect using JS:

echo "<script> window.location.href = 'index.php'; </script>";

Or in PHP:

header('Location: index.php');
exit;