I am having a little issue with echoing javascript dynamically via php. Here is my code
$url = "http://www.newsite.com";
echo "
<html>
<head>
<title>Redirecting</title>
</head>
<body onload='redirect()'>
Not Logged In
<script type = 'text/javascript'>
function redirect() {
window.location=".$url."
}
</script>
</body>
</html>
";
My javascript console is telling me that "redirect()" cant be found (Uncaught ReferenceError: redirect is not defined)
Any ideas what's causing this?
Drop that client-based redirect entirely. Use:
header("HTTP/1.0 302 Moved Temporarily");
header("Location: $url");
You're missing a quotation mark. This will fix your issue:
function redirect() {
window.location='".$url."';
}
Currently, your page is rendered as follows (note the missing quotes / syntax error):
function redirect() {
window.location=http://www.newsite.com;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With