0

I'm creating a mobile app using jQuery mobile. I want a user to be redirected to a specific page on entering a search query. For example, if a user types xyz, he should be redirected to #xyz.

My code:

<form onsubmit="queryRedirect()">
    <input type="search" id="search_bar" placeholder="Type a string">
</form>

function queryRedirect() {
var page = document.getElementById('search_bar').value;
$( ":mobile-pagecontainer" ).pagecontainer( "change", "#" + page);
return false;
}

It does redirect, as expected, but the problem is, that the new page lasts only for 1-2 seconds, and then it automatically comes back to the home page.. Plz help me solve this..

1
  • Just add preventDefault() at the beginning of the function to prevent page from redirecting when form is submitted.
    – Omar
    Aug 5, 2015 at 22:40

1 Answer 1

-1

I think this will do.

Use location.href = '#'+page;

Check out this fiddle.

Here is the snippet.

function queryRedirect() {
  var page = document.getElementById('search_bar').value;
  location.href = '#' + page;
  return false;
}
<form onsubmit="return queryRedirect()">
    <input type="search" id="search_bar" placeholder="Type a string" />
</form>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id='xyz'>
    hh
</div>

2
  • This part of code doesn't even use jQuery. That doesn't seem to be a problem. Aug 5, 2015 at 18:04
  • And it is working fine on Chrome on my Android mobile. Note that I have changed the onsubmit attribute's value. I have added return before the function name. That prevents it from submitting the form. Aug 5, 2015 at 18:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.