0

I would like to execute a javascript code within the normal header tags, before I redirect the user to another website, but I cannot figure it out how to actually make it work so the code will be executed and the visitor will be redirected.

I want the following:

<html>
 <head>
  //script here
 </head>
 <body>
 </body>
</html>

<?php header ('Location: example.com') ?>

Can someone please tell me how to do it correctly, so it will not return any 'Headers already sent' errors?

Taboo Loco
  • 551
  • 3
  • 9
  • 14

6 Answers6

4

Use output buffering:

<?php

ob_start();
echo "Hello\n";

setcookie("cookiename", "cookiedata");

ob_end_flush();

?>
John Conde
  • 217,595
  • 99
  • 455
  • 496
2

If visitors get to this page by following links from your site, you could send additional data to let your server script know that JavaScript is enabled; e.g.

$('a').each(function() {
    var url = this.href;

    if (url.indexOf("?") !== -1)
        this.href = url + '&js=1';
    else
        this.href = url + '?js=1';
});

This will change all links to include a special parameter that indicates whether JavaScript is enabled or not. If the additional JavaScript code you wish to run can be placed here as well, that would be even better!

The server script will use something like if (!empty($_GET['js'])) { ... } to either write a piece of JavaScript (old answer 2) or perform a redirect straight away.

Old answer

You can send a Refresh header in case JavaScript is disabled.

<?php

    header('Refresh: 2; url=http://www.example.org');

?>
<html>
 <head>
  //script here
 </head>
 <body>
 </body>
</html>

I have to admit that I did not try this, but there's a good chance it may work

Old answer 2

If you're sure that JavaScript is enabled, though, you could let JavaScript perform the redirect after it's done; if the url is always the same, you leave out the PHP code and just use a static string:

<html>
 <head>
  <script>
  //script here
  location = <?php echo json_encode('http://www.example.org'); ?>;
  </script>
 </head>
 <body>
 </body>
</html>
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • Thank you. However in this case the visitor will not be redirected if they do not have Javascript enabled browser? Or I'm wrong? – Taboo Loco Jan 26 '13 at 16:26
  • yes jack you are correct :D i upvoted you because finally you are correct :D – Patt Mehta Jan 26 '13 at 16:30
  • @TabooLoco Updated the answer with a different approach. – Ja͢ck Jan 26 '13 at 16:30
  • Is there any quicker way, than waiting 1 second to refresh? I don't want the visitor to see any of the page, just get redirected right away, like it would be an instant redirect. – Taboo Loco Jan 26 '13 at 16:37
  • @TabooLoco well, how do people end up on this page? do they follow links from another page on your site or do they come from an external link of another site? – Ja͢ck Jan 26 '13 at 16:39
  • They actually comes from a link I provide them, and when they click on it, it redirects them to an outside link. – Taboo Loco Jan 26 '13 at 16:44
  • @ColeJohnson thanks, though you reversed the condition, so I've fixed that :) – Ja͢ck Jan 27 '13 at 22:05
2


content="<seconds>;url=http://www.the-url.com"
<head>
    <meta http-equiv="REFRESH" content="10;url=http://www.the-domain-you-want-to-redirect-to.com">
</head>
<body>
    <script>
        alert("run before redirect");
    </script>
</body>

http://jsfiddle.net/W9dhW/

divyaSharma
  • 133
  • 2
  • 11
Patt Mehta
  • 4,110
  • 1
  • 23
  • 47
  • I try my level best not to, but I have seen there is a "cattle herd" movement when it comes to upvotes, new members rarely receive upvotes here. If you are not popular here, there are no votes for you :( – Patt Mehta Jan 26 '13 at 16:42
  • The problem with this is that some search engines don't like REFRESH – Cole Tobin Jan 27 '13 at 19:04
0

This is not possible with a Location header. If your response includes one the browser will be redirected and no Javascript will be executed.

If you want to do something on the client side, either do it before fetching the URL that redirects or do it after landing on the final target URL.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

Redirect user to other website after executing some JavaScript

<script type='text/javascript'>
    alert('executing some javascript');

    // redirecting to other website
    window.location.href = "http://example.com";
</script>

You don't need (and cannot use) PHP to achieve what you require

lostsource
  • 21,070
  • 8
  • 66
  • 88
0

you have to use the php header before html starts

<?php header ('Location: example.com') ?>
<html>
 <head>
  //script here
 </head>
 <body>
 </body>
</html>

note: if you are using <?php header('Location:example.com'); ?> the following lines are meaning less since it will be redirected to another page at very beginning line.

Thirumalai murugan
  • 5,698
  • 8
  • 32
  • 54