Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-12-2013, 10:17 PM   PM User | #1
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
should I return false? and what happened to the click?

hello,

I'm working on a greasemonkey script that has a form (that isn't really a form, but just a collection of inputs - otherwise I guess I would be using beforesubmit) which is submitted somehow (dunno how, hoping not to have to find out) on the click of a button.

What I want to do is do a validation on the "form" before it gets sent. I struggled for a while because I was adding a click listener to the button (making the validation happen after the info was sent), but I realised that using "mousedown" I get to sneak in, and do the validation. I then just programatically click the submit button and away it goes.

Which is great. Works fine on my machine, anyway. But I have two doubts. A validation error brings up a confirm. If you click OK, the form sends. If you click cancel it doesn't. But shouldn't there be a return false in there somewhere, even though you would be returning false from the mousedown which I guess is unrelated to the click?

And that's the second question: The way I understand it, the event order is mousedown, click, mouseup. So what happens to the click event? Does it die while the confirm box is showing?

Here's a very basic example which demonstrates this. It's in jQuery because the page uses jQuery and I have to attach listeners to the dynamically created button (which I have no control over) - I know I can do that in vanilla, but it's just easier this way. But anyway, this isn't a jQuery question - the jQuery works fine. What I'm asking is how the javascript works here. More than anything, why don't I need to be canceling the click event in some way?

Any thoughts much appreciated.

Code:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> 
</head>

<body>
<input type="button" id="start" value="show form"/> 
<script type="text/javascript">
$(document).ready(function () {

    // these bits get done by the page - I have no control at this point

    $("#send").live("click", function () {
        $('#theform').remove();
		alert("form sent!");
    });

    $("#start").click(function () {
        $('body').append('<div id="theform"><input type="checkbox" id="cb"/>Check me or you\'ll get an alert<br>' +
            '<input type="button" id="send" value="send form"/></div>')
    });

    // this is where the greasemonkeying starts

    $("#send").live("mousedown", function () {
        if (!($("#cb").is(":checked"))) {
                if (confirm("You didn't check the box! Sure you want to continue?")) {
                $("#send").click();
            }
        }
    });

});

</script>
</body>
</html>
xelawho is offline   Reply With Quote
Old 01-13-2013, 12:40 AM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Code:
if (confirm("You didn't check the box! Sure you want to continue?")) {
The confirm returns true or false, so this could be written:

Code:
if (confirm("You didn't check the box! Sure you want to continue?") == true) {
Yes, the click event is absorbed by the confirm dialog. [As I recall, it wasn't always; I believe some browsers used to propagate it - that is, pass it on.]
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 01-13-2013, 01:07 AM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,468
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by xelawho View Post
And that's the second question: The way I understand it, the event order is mousedown, click, mouseup. So what happens to the click event? Does it die while the confirm box is showing?
how can a click occur before the mouse button goes up? Note that if you click a button, then drag to ,say the desktop, and release the button, click() is not fired.

i don't think you can actually terminate/prevent a click() from a mousedown...


the reason confirm/alert help is that it prevents the mouseup on the element with the click() event, thus ensuring that the button is not actually clicked.



also , this test app shows this output:

Code:
EVT: mousedown
EVT: mouseup
EVT: click




you may want to stop pulling your hair; i found this text on the jQuery site:
Quote:
Since the .live() method handles events once they have propagated to the top of the document, it is not possible to stop propagation of live events. Similarly, events handled by .delegate() will propagate to the elements to which they are delegated; event handlers bound on any elements below it in the DOM tree will already have been executed by the time the delegated event handler is called. These handlers, therefore, may prevent the delegated handler from triggering by calling event.stopPropagation() or returning false.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%

Last edited by rnd me; 01-13-2013 at 01:09 AM..
rnd me is offline   Reply With Quote
Old 01-13-2013, 01:48 AM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
Quote:
Originally Posted by rnd me View Post
this test app shows this output:
Code:
EVT: mousedown
EVT: mouseup
EVT: click
Interesting. I figured it was otherwise for some reason. I should really look some of this stuff up. But as long as nobody uses mousedown, my reign of greasemonkeying terror can continue.

So, nobody use mousedown, please

There was no hair-pulling going on. That it works is good enough for me. That it should work on non-windows OSs is even better. Thanks again for the info
xelawho is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:27 PM.


Advertisement
Log in to turn off these ads.