28
<p><a href="index.html" data-role="button" data-icon="arrow-r" data-iconpos="right" data-theme="a" onclick="doSomething(); return false">VERIFY</a></p>

I am using the above code which is a jQM button with the onClick() set. The onclick is called and doSomething() is executed but after that, jQM shows the "error loading page" message.

How can I supress the error? In this case I want the jQM button but don't want it to change page.

Thanks

2
  • why not remove the href? Commented Feb 7, 2013 at 9:07
  • So glad I finally stumbled upon this question, I kept getting "synchronous xml error" which totally put me on the wrong track... Commented May 17, 2016 at 12:48

4 Answers 4

45

Since you are using jQuery I would recommend to use jQuery to wire up your events as well. With that being said using e.preventDefault(); and e.stopImmediatePropagation(); should stop jQuery mobile from performing the default action on the <a/>.

$("#verify").click(function (e) {
    e.stopImmediatePropagation();
    e.preventDefault();
    //Do important stuff....
});

Update

The better way to use your existing markup would be to simply add rel="external" to your <a/> And your onclick should behave correctly.

<p>
  <a href="index.html" data-role="button" data-icon="arrow-r" data-iconpos="right" data-theme="a" onclick="doSomething(); return false" rel="external">VERIFY</a>
</p>

This will work since jQuery Mobile will treat the link as a normal <a/> tag and return false will simply stop the default action.

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

1 Comment

That rel="external" ... is worth 6 jQM-n00b-hours
6

I think your problem is that you have multiple actions on your button and are using a anchor tag. When clicking the button you're invoking the page to transition to index.html and a onClick event.

<a 
    href="index.html"                       <-- go to index.html
    data-role="button" 
    data-icon="arrow-r" 
    data-iconpos="right" 
    data-theme="a" 
    onclick="doSomething(); return false">  <-- Click event
VERIFY
</a>

Might try (might need to remove/add some other attributes)

<input
    type="button"
    name="verify"
    id="verify"
    data-icon="arrow-r" 
    data-iconpos="right" 
    data-theme="a" 
    value="VERIFY" />

and now add a click event

$('#verify').click(function() {
    alert('Button has been clicked');
});

Live Example: http://jsfiddle.net/vRr82/2/

3 Comments

This isn't working. I think it's because jQM hides the <input> button and shows an styled <a> tag instead, so you're clicking the <a> not the <input> (which has the click event).
This is the correct solution. Also just removing the href should also work.
I am using cefsharp browser in my form application. I tried your code which is working fine can you please tell how can I load another HTML page from project resources on this button click OR how can I call a c# method on this button click. Thanks :-)
0

I think you should be append data-ajax="false" inside anchor tag.. because in jQuery mobile the page transition is done by AJAX, and for page transition it must be false..

Comments

-1

Use Jquery live Function. That is prety much useful for me

1 Comment

Besides that I don't see how that answers the question as of jQuery 1.7 live() has been deprecated.

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.