Keith Donegan Keith Donegan - 1 year ago 77
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 Source

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");
});