简体   繁体   中英

Javascript “window.location” stopping PHP redirect

Basically there's a callto function, and a timer. The timer is displayed visually via Javascript countdown, and at the same time there's a PHP redirect which has been set to the same length as the Javascript countdown. However, the window.location callto function is preventing the PHP header redirect from working. Can anybody tell me why?

The Javascript window.location function (located after body due to it preventing code below it from being processed:

<?
echo"
<script type='text/javascript'>
    window.location='callto:".$to."';
</script>";
?>

The PHP header redirect (above the html tag):

<?php

header( "refresh:5;url=wherever.php" );

?>

If I remove the window.location function the redirect works.

you should redirect just with javascript, don't redirect with php. why doing a php redirect when you're doing it already with javascript?

In order to delay the redirect you should use Javascript setTimetout() function, such as:

setTimeout(function () {document.location = 'detination';}, time_in_miliseconds);
 header( "refresh:5;url=wherever.php" );

This is NOT a valid redirecting header!

header('Location: http://example.com/sub/script.php');

THIS is a valid Redirect Header!

To make the script wait as you want , you'll need to add a Sleep on it:

sleep(5);
header('Location: http://example.com/sub/script.php');

Instead of a javascript redirect you should render an call-link with simple html and the href="callto:...". If you want to show the call screen without user interaction you only have to trigger a click event on the link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM