[英]window location href not working
I am trying to navigate to a new page using javascript. 我正在尝试使用javascript导航到新页面。 When I put window.location.href = "next.html" in the header of a page it works fine.
当我在页面的页眉中放置window.location.href =“ next.html”时,它可以正常工作。 but when calling it from a function after a button is clicked, it does not work.
但是当单击按钮后从函数调用它时,它不起作用。 Example code:
示例代码:
function next(){
$post = "?id=10"
alert($post);
window.location.href = "next.html" + $post;
}
the alert displays ?id=10, but it does not redirect. 警报显示?id = 10,但不会重定向。 Thanks for the help
谢谢您的帮助
The " missing was a typo and is not in the real site, sorry! 缺少的是错字,不在实际网站中,抱歉!
This: 这个:
function next(){
$post = "?id=10
alert($post);
window.location.href = "next.html" + $post;
}
Is a syntax error in your PHP. 是PHP中的语法错误。 Try this:
尝试这个:
<?php
function next(){
$post = "?id=10";
?>
alert($post);
window.location = "next.html" + <?php echo $post; ?>;
<?php
}
?>
EDIT : Ok after confusion has been cleared - if no PHP is involved try: 编辑 :清除混乱后可以-如果不涉及PHP,请尝试:
function next(){
var post = "?id=10";
alert(post);
window.location = "next.html" + post;
}
Seems like there is a error in the code: 似乎代码中有错误:
$post = "?id=10
should have "
in the end $post = "?id=10
应该有"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.