简体   繁体   English

Javascript窗口位置刷新页面

[英]Javascript window location refreshes page

I am trying to change the window url using window.location but it seemes like it doesn't work. 我正在尝试使用window.location更改窗口URL,但似乎不起作用。

function redirect() {
  var r = confirm('Are you sure you want to log out ?');
  if (r) {

    window.location.href = "http://localhost/mypage.php";
    return false;
  } else {
    alert("failed");
    return false;
  }
}

When i press ok, the page refreshes.. 当我按OK时,页面刷新。

window.history.pushState('AnotherPage','title','/ pageUrl');

Your code is correct. 您的代码是正确的。 I have tested it and work fine. 我已经测试过并且工作正常。 Check your others scripts maybe some interference. 检查您的其他脚本,可能会有所干扰。

!IMPORTANT! !重要! Check also if your trigger button is not submit type ... if is submit type not working ... So if is this your problem must manipulate the button type to button inside your function. 还要检查您的触发按钮是否不是提交类型...如果提交类型不起作用...那么如果这是您的问题,则必须在函数内部对按钮类型进行操作。

... if (r) {
  document.getElementById('your_button').setAttribute("type", "button");
...

Try this: window.location : 试试这个: window.location

function redirect() {
  var r = confirm('Are you sure you want to log out ?');
  if (r) {

    window.location = "http://localhost/mypage.php";
    return false;
  } else {
    alert("failed");
    return false;
  }
}

call it on onclick="redirect();" 调用onclick="redirect();" in button. 在按钮上。

See example here : Javascript Page Redirect 在此处查看示例: Javascript页面重定向

The redirect should work fine with the following code (without the need for href): 使用以下代码,重定向应该可以正常工作(不需要href):

window.location = "http://localhost/mypage.php";

Another thing to check is that the redirect URL is pointing to the correct location ie are there any subdirectories that need to be included in the path? 要检查的另一件事是重定向URL指向正确的位置,即路径中是否需要包含任何子目录? eg " http://localhost/folder/mypage.php "? 例如“ http://localhost/folder/mypage.php ”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM