Keith Donegan Keith Donegan - 3 months ago 11
Javascript Question

Redirecting a page using Javascript, like PHP's Header->Location

I have some code like so:

$('.entry a:first').click(function()
{
<?php header("Location:" . "http://www.google.com"); ?>
});


I would like to know how I can achieve this using Javascript.

Answer

The PHP code is executed on the server, so your redirect is executed before the browser even sees the JavaScript.

You need to do the redirect in JavaScript too

$('.entry a:first').click(function()
{
    window.location.replace("http://www.google.com");
});
Comments