echo Javascript window.location.href not working
echo, javascript, navigation, php
Solution
Your browser treats the response as plaintext.
Prepend to you response a `Content-Type: text/html\`n plus wrap your content inside an `<html></html>` tag.
Problem
I have a function which echoes javascript to navigate to a different page. While navigation occurs, the ``` echo 'window.location.href="'.$url.'";'; ``` does not work and simply prints it on the screen. ``` "window.location.href="./index.php"; ``` I use my function this way: `redirect("./index.php");` My php function is as follows ``` function redirect($url) { if (!headers_sent()) { header('Location: '.$url); exit; } else { echo '<script type="text/javascript">'; echo 'window.location.href="'.$url.'";'; echo '</script>'; echo '<noscript>'; echo '<meta http-equiv="refresh" content="0;url='.$url.'" />'; echo '</noscript>'; exit; } } ```