0

I'm sort of new to JQuery, but I'm practicing everyday. My goal is to open the link after the buttons have been clicked but the link doesn't seem to be opening. I'm trying to open the link inside the if statement so everything happens accordingly.

  window.setInterval(function(){ 
    if ($('#add-remove-buttons').find('.button').length > 0) {
        $('#size').val($('#size option').filter(function(ind, el) {
           return $(el).text() === 'Large';
        }).val());  
        $('#add-remove-buttons').find('.button').trigger('click'); 
        setTimeout(function() { 
          window.location.replace('http://myweblink'); 
         }, 900); 
        } 
   }, 100);

EDIT (STILL NEED HELP)

I've tried changing it but it doesn't load. I think it might be getting stuck in the 100ms loop. I put the function in a 100ms loop so it can detect if ($('#add-remove-buttons').find('.button').length > 0) I also just realized that after the user clicks the button, this html automatically appears:

<fieldset id="add-remove-buttons"><input class="button remove" name="commit" value="remove" type="submit"><a href="/shop" class="button continue">keep shopping</a></fieldset>

This means that the if statement : if ($('#add-remove-buttons').find('.button').length > 0) from my code, becomes false and the code for changing the URL doesn't run. Is there a way to detect the presence of the html code above like the if statement that became false? After I figure that out, I can put the window.location.href = "http://myweblink"; and then get it to work!

MGames
  • 1,171
  • 2
  • 13
  • 24

3 Answers3

1

And in your code it is missing the complete web address.

Use

 window.location.replace('http://myweblink.com'); 

Instead

 window.location.replace('http://myweblink'); 

To redirect,jQuery is not necessary, and window.location.replace(...) will best simulate an HTTP redirect.

It is better than using window.location.href =, because replace() does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco. If you want to simulate someone clicking on a link, use location.href. If you want to simulate an HTTP redirect, use location.replace.

For example:

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

You can read the answer here.

Community
  • 1
  • 1
Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140
0

Try window.location.href = "http://your.wesite.com"; This will replace your address bar.

Shubham
  • 1,755
  • 3
  • 17
  • 33
0

If you're using jQuery, just use $(location).attr('href',url);.

window.location.href seems to have inconstant behavior in some browsers, in fact, it flat out doesn't work in my version of Firefox.

Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58