窗口位置href不起作用

时间:2013-01-02 04:26:03

标签: javascript redirect web

我正在尝试使用javascript导航到新页面。当我将window.location.href =“next.html”放在页面的标题中时,它可以正常工作。但是在单击按钮后从函数调用它时,它不起作用。示例代码:

function next(){
 $post = "?id=10"
        alert($post);
     window.location.href = "next.html" + $post;
 }

警报显示?id = 10,但不会重定向。谢谢你的帮助

“失踪是一个错字,不在真实的网站,抱歉!

2 个答案:

答案 0 :(得分:5)

此:

function next(){
 $post = "?id=10
        alert($post);
     window.location.href = "next.html" + $post;
 }

PHP中存在语法错误。试试这个:

<?php
    function next(){
     $post = "?id=10";
?>
     alert($post);
     window.location = "next.html" + <?php echo $post; ?>;
<?php
    }
?>

你真的不应该像这样将PHP与JavaScript混合在一起。


编辑:清除混淆后确定 - 如果不涉及PHP,请尝试:

function next(){
 var post = "?id=10";
        alert(post);
 window.location = "next.html" + post;
 }

答案 1 :(得分:0)

似乎代码中存在错误:

$post = "?id=10最后应该有"