admin管理员组

文章数量:1391947

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.

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.

Share Improve this question edited Jan 27, 2018 at 8:15 Jomla asked Jan 27, 2018 at 4:18 JomlaJomla 931 silver badge9 bronze badges 6
  • 1 Try console logging response prior to your conditional in the .post() method's callback function. Is it boolean true or the string "true"? Comparing boolean true to string "true" evaluates to boolean false, so in that case your else condition will be evaluated. – SimianAngel Commented Jan 27, 2018 at 4:44
  • please, add result of console.log(url,responce) in start of 'function(responce)' – Petrashka Siarhei Commented Jan 27, 2018 at 4:57
  • Please check my updated question in above, thank you. – Jomla Commented Jan 27, 2018 at 6:11
  • <div class='alert alert-success'> You have registered successfully.</div> – Jomla Commented Jan 27, 2018 at 6:34
  • Add your full html. pleease – Petrashka Siarhei Commented Jan 27, 2018 at 7:15
 |  Show 1 more ment

3 Answers 3

Reset to default 2

window.location work if you use code for example:

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

You can look: https://developer.mozilla/en-US/docs/Web/API/Window/location

window.top.location = '[your URL]' is what you want.

Below code works like magic

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

本文标签: jquerywindowlocationreplace not working in JavaScriptStack Overflow