使用Javascript重定向页面,如PHP的Header-> Location

Kei*_*gan 11 javascript redirect

我有一些像这样的代码:

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

我想知道如何使用Javascript实现这一目标.

rle*_*mon 29

你不能用JS和PHP混合,PHP在页面发送到浏览器之前呈现(即在JS运行之前)

您可以使用window.location更改当前页面.

$('.entry a:first').click(function() {
    window.location = "http://google.ca";
});
Run Code Online (Sandbox Code Playgroud)


kla*_*her 14

PHP代码在服务器上执行,因此在浏览器甚至看到JavaScript之前执行重定向.

您还需要在JavaScript中进行重定向

$('.entry a:first').click(function()
{
    window.location.replace("http://www.google.com");
});
Run Code Online (Sandbox Code Playgroud)

  • @KeithDonegan是..`window.open("http://www.google.com");`这是一些非常基本的JS. (2认同)