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?
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.
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
Refreshheader in case JavaScript is disabled.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: