-1

I've read the similar questions on SO but none of them helps.

I have this JS

$(document).keypress(function(e) {
    if(e.which == 13) {



          var txt = $('#srch').val();

    if (txt.toUpperCase() === "abc".toUpperCase()){

      window.location= "http://somepage.html";



    }
}
});

Its redirecting only on the index page. On the rest of the pages it's not redirecting. It's only putting a "?" on the end of the current URL.

nailoxx
  • 9
  • 6

2 Answers2

0

Check console (and code):

  1. any earlier error blocking your code
  2. input#srch exists in every page
  3. is an https page requesting a non https page? (mixed content blocked by browser)
  4. your js code exists in every page
  5. does exist an earlier bind to same input#srch?
  6. url is always the same or is dynamic? if so, is correctly populated?
Diego Betto
  • 721
  • 6
  • 19
  • I checked all suggestions but that's not the case. If i put an alert right after ` if (txt.toUpperCase() === "abc".toUpperCase()){ alert("works"); ` its firing so its all working except the actual redirect. – nailoxx Jul 13 '16 at 07:00
  • Can you provide an url? – Diego Betto Jul 13 '16 at 07:43
  • sorry, I can't for security reasons. I tried to redirect to google.com but still the same. the only thing that happens when I press enter is its adding a "?" at the end of the current URL – nailoxx Jul 13 '16 at 08:01
  • Can you try with different browsers? Same behaviour? – Diego Betto Jul 13 '16 at 08:04
  • It works in firefox but its not working in chrome and Edge – nailoxx Jul 13 '16 at 08:14
  • try replacing window.location = 'http://....'; with window.location.href = 'http://....'; or window.location.assign('http://....'); – Diego Betto Jul 13 '16 at 08:44
0

It was working in firefox but not in chrome and edge. I added return false; like this

 window.location= "http://somepage.html"; return false;

Now it works for all browsers. Thanks to Diego Betto for pointing me in the right direction.

nailoxx
  • 9
  • 6