Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call ondblclick without calling onclick-event

Is there a simple solution without jquery, inside HTML-tags, that will call the ondblclick without causing onclick to happen too?

This always closes the window although it should just show the alert:

<a href="#" ondblclick="alert('dbl')" onclick="window.close();">X</a>

(it only works in a javascript popup, cause you cannot close the main window with window.close();)

like image 530
rubo77 Avatar asked Sep 09 '13 13:09

rubo77


2 Answers

My guess is that every solution will also have the problem with the doubleclick timeout-length varying on user preferences described by Irvin Dominin aka Edward in the comment above:

From jQuery docs but related: "It is inadvisable to bind handlers to both the click and dblclick events for the same element. The sequence of events triggered varies from browser to browser, with some receiving two click events before the dblclick and others only one. Double-click sensitivity (maximum time between clicks that is detected as a double click) can vary by operating system and browser, and is often user-configurable."

I abandoned using doubleclick and used CTRL+click instead:

<a href="#" onclick="if(event.ctrlKey) { 
 alert('CTRL+Mouseclick'); return false; 
} else { window.close(); }">X</a>

see:

Catch onclick-event with CTRL pressed

like image 164
rubo77 Avatar answered Nov 16 '22 05:11

rubo77


I have solved this with the below code,

<a id="press" href="javascript:void(0)" onclick="singleClick(event)"
    ondblclick="doubleClick(event)">Click here</a>

<div id="log"></div>

My JavaScript will be ,

    var timer;
    var status = 1;

    function singleClick(event) {
        status = 1;
        timer = setTimeout(function() {
            if (status == 1) {
                document.getElementById("log").innerHTML ='I  am single click !';
            }
        }, 500);

    }

    function doubleClick(event) {
        clearTimeout(timer);
        status = 0;
        document.getElementById("log").innerHTML = 'I  am a double  click!';
    }

Please let me know the status.Hope this helps.

like image 5
Human Being Avatar answered Nov 16 '22 06:11

Human Being