Broadbiz Broadbiz - 2 months ago 9
HTML Question

JQuery window.location only works on a double click, how can i get it to work on the first click?

I am using JQuery to open another page when the user clicks on a link (The site needs to run some script before the user is redirected).

The website redirects properly but only when the link is clicked twice. is there an issue with my code? Have I done something wrong?



$(document).ready(function() {
$.form_changed = 'false'; //variable set to false as it is quicker.
$(".save_alert").click(function() {
if ($.form_changed == 'true') {
//modal.style.display = "block";
} else {
$(".your-properties").click(function() {
window.location.href = "http://www.stackoverflow.com/";
})
$(".your-preferences").click(function() {
window.location.href = "http://www.google.com/";
})
$(".property-matches").click(function() {
window.location.href = "http://www.bing.com/";
})
$(".your-interests").click(function() {
window.location.href = "http://www.ask.com/";
})
}
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul id="header_tabs">
<a href="#" id="green" class="save_alert your-properties" title="Click to manage your Properties">
<li id="green">Properties</li>
</a>
<a href="#" id="pink" class="save_alert your-preferences" title="Click to manage your Preferences">
<li id="pink">Preferences</li>
</a>
<a href="#" id="orange" class="save_alert property-matches" title="Click to manage your Matches">
<li id="orange">Likes</li>
</a>
<a href="#" style="margin-right: 0;" class="save_alert your-interests" id="plum" title="Click to manage your Communications">
<li id="plum">Communications</li>
</a>
</ul>





Any help is appreciated.

Answer Source

Here is your solution:

  $(document).ready(function() {
      $.form_changed = 'false'; //variable set to false as it is quicker.
      $(".save_alert").click(function() {
        if ($.form_changed == 'true') {
          //modal.style.display = "block";
      } else {
      if($(this).hasClass('your-properties')) {
        window.location.href = "http://www.stackoverflow.com/";
      }
      if($(this).hasClass('your-preferences')) {
        window.location.href = "http://www.google.com/";
      }
      if($(this).hasClass('property-matches')) {
        window.location.href = "http://www.bing.com/";
      }
      if($(this).hasClass('your-interests')){
        window.location.href = "http://www.ask.com/";
      }
    }
  });
});