Adam Tuttle
Adam Tuttle

Reputation: 19824

jquerymobile confirm doesn't seem to be affecting outcome?

Here's a snippet from a jquerymobile application. It's a simple file-manager that allows files to be deleted, and I want to confirm the delete. I'm attempting to use a javascript confirm to double check whether an action should continue; but it seems that no matter what I do, I can't stop it from happening.

<script type="text/javascript">
    $(document).ready(function(){
        $(".confirm").live("click",function(ev){
            var nam = $(this).html(),
                loc = $(this).attr('href'),
                answer = confirm("Are you sure you want to delete:\n\n" + nam);
            if (answer){
                return true;
                //window.location.href = loc;
            }else{
                ev.preventDefault();
                return false;
                window.location.href = window.location.href;
            }
        });
    });
</script>

...

<div data-role="page" data-theme="a">
    <div data-role="content">
        <ul data-role="listview" data-inset="true">
            ...
            <li><a class="confirm" href="?delete=#urlEncodedFormat(f.name)#">#f.name#</a></li>
        </ul>
    </div>
</div>

You can see that I've tried 3 different methods to prevent the link from redirecting the page, but none of them seem to be doing what I had hoped. What am I doing wrong?

Upvotes: 0

Views: 568

Answers (1)

naugtur
naugtur

Reputation: 16915

You're using .live without understainding how it does the magic.

Change .live to .bind and it will work.

Also - read about .live to get the knowledge that .live binds to document and catches the events as they bubble up, so it's far too late to prevent default actions or anything.

And BTW - if you put code after a return statement there is this slight possibility that somebody could get confused into thinking that it is actually being run for some weird reason.

[edit]

If you want it to work without javascript (progressive enhancement and stuff) then you should go with dialogs generated server-side for the purpose of asking the question even if there's no javascript.

If you don't care about progressive enhancement and want it to work only in high-end browsers, then why do you bother with a href? rename href into data-href and load it if the user confirms. You won't have to prevent anything.

Upvotes: 2

Related Questions