2

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.

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. Commented Jan 27, 2018 at 4:44
  • please, add result of console.log(url,responce) in start of 'function(responce)' Commented Jan 27, 2018 at 4:57
  • Please check my updated question in above, thank you. Commented Jan 27, 2018 at 6:11
  • <div class='alert alert-success'> You have registered successfully.</div> Commented Jan 27, 2018 at 6:34
  • Add your full html. pleease Commented Jan 27, 2018 at 7:15

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

Below code works like magic

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.