js中php的标头位置的最佳替代品是什么


What is the best replace for header location of php in js?

相当于php头位置函数

<?php 
header("location:somepage.php");
?>

在JavaScript中?javascript 是否有可以像标头函数一样做同样的事情?如果是,那是什么?

JavaScript 等效项是;

window.location.href = "somepage.php"

更多信息: http://www.tutorialspoint.com/javascript/javascript_page_redirect.htm

您还可以使用 meta 标签立即将用户重定向到另一个页面,无需 javascript:

<html>    
  <head>      
    <title>Your Title</title>      
    <meta http-equiv="refresh" content="0;URL='http://example.whatever.com/'" /> 
  </head>    
  <body> 
      <!-- NO CONTENT -->
  </body>  
</html>  

这是客户端操作,而不是服务器重定向。

Javascript for redirect:

window.location="http://www.example.com";

更多信息: http://www.tutorialspoint.com/javascript/javascript_page_redirect.htm

Javascript在你的浏览器中执行,所以它不能做与你请求具有重定向的php时发生的情况完全相同的事情。如果您想要的只是访问另一个页面,而无需向服务器发出请求,这就足够了:

window.location.href = "somepage.php"

在 Javascript 中,等效于 header:

索引.html(或索引.php)

....some html code
<script> 
  window.location="example.php";
</script>

请记住,该文件示例.php必须位于本地服务器的目录中(如果您使用 XAMPP 或 WAMPP)。