简体   繁体   中英

window.location.replace not working in JavaScript

Based on my JavaScript code, window.location doesn't work after receiving true response. 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.href = 'url link';
  • window.location(url);
  • window.location.href = 'url Link'; return false;
  • window.location(url); return false;
  • window.location.replace(url); return false;
  • window.top.location = '[url]'
  • window.top.location = '[url]'; return false;
  • location.assign("url");
  • location = "url";
  • location.reload(true);

but In the THIS SECTION WORKS , all above and following lines codes works. I mean after else means response is false. ((I don't need it in false response but it works!!))

FYI , response False and True both of them works well.

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();

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

  • $('form[name="headersignupform"]')[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.

window.location work if you use code for example:

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

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

Below code works like magic

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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