简体   繁体   English

window.location.replace 在 JavaScript 中不起作用

[英]window.location.replace not working in JavaScript

Based on my JavaScript code, window.location doesn't work after receiving true response.根据我的 JavaScript 代码,window.location 在收到真实响应后不起作用。 However I've used possible ways to use it but it seems there's something wrong or maybe I'm doing wrong.但是,我已经使用了可能的方法来使用它,但似乎有问题,或者我做错了。

Here is my code:这是我的代码:

  $(function() {
  var url = $(".url").val();
  // start sign up functionality
  $(".signupbtn").on('click', function() {
      $('html, body').animate({
          scrollTop: $(".toppage").offset().top - 100
      }, 'slow');
      $.post("<?php echo base_url();?>account/signup",
          $("#headersignupform").serialize(),
          function(response) {
              if ($.trim(response) == 'true') {
                  $(".resultsignup").html(response);
                  //THIS SECTION NOT WORKING
              } else {
                  $(".resultsignup").html(response);
                 // THIS SECTION WORKS
              }
          });
  });
  // end signup functionality
  })

In the THIS SECTION NOT WORKING which I mentioned in above, I've used all these following lines, but it doesn't work after true response:在我上面提到的本节不工作中,我使用了以下所有行,但在真正响应后它不起作用:

  • window.location.replace(url); window.location.replace(url);
  • window.location.href = 'url link'; window.location.href = '网址链接';
  • window.location(url); window.location(url);
  • window.location.href = 'url Link'; window.location.href = 'url 链接'; return false;返回假;
  • window.location(url); window.location(url); return false;返回假;
  • window.location.replace(url); window.location.replace(url); return false;返回假;
  • window.top.location = '[url]' window.top.location = '[url]'
  • window.top.location = '[url]'; window.top.location = '[url]'; return false;返回假;
  • location.assign("url"); location.assign("url");
  • location = "url";位置=“网址”;
  • location.reload(true); location.reload(true);

but In the THIS SECTION WORKS , all above and following lines codes works.但在本节 WORKS 中,所有上面和下面的代码都有效。 I mean after else means response is false.我的意思是在 else 之后意味着响应是错误的。 ((I don't need it in false response but it works!!)) ((我不需要在错误响应中使用它,但它有效!!))

FYI , response False and True both of them works well.仅供参考,响应 False 和 True 两者都运行良好。

Why I need this?为什么我需要这个? Because after true response I need to reset form fields, for this issue, I also used these following lines, but again not working:因为在真正响应后我需要重置表单字段,对于这个问题,我还使用了以下几行,但再次不起作用:

  • $('#headersignupform')[0].reset(); $('#headersignupform')[0].reset();

  • document.getElementById("headersignupform").reset(); document.getElementById("headersignupform").reset();

  • $('form[name="headersignupform"]')[0].reset(); $('form[name="headersignupform"]')[0].reset();

  • $("#headersignupform").get(0).reset(); $("#headersignupform").get(0).reset();

Solution :解决方案

I've solved the issue by adding response as HTML into JavaScript not through posted HTML from Ajax.我通过将响应作为 HTML 添加到 JavaScript 而不是通过从 Ajax 发布的 HTML 解决了这个问题。

window.location work if you use code for example: window.location 工作,如果你使用代码例如:

location.assign("http://www.mozilla.org"); // or
location = "http://www.mozilla.org";

You can look: https://developer.mozilla.org/en-US/docs/Web/API/Window/location你可以看看: https : //developer.mozilla.org/en-US/docs/Web/API/Window/location

window.top.location = '[your URL]'就是你想要的。

Below code works like magic下面的代码像魔术一样工作

window.history.pushState("", "", "new url"); window.history.pushState("", "", "new url"); window.location.reload(); window.location.reload();

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

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