Skip to content Skip to sidebar Skip to footer

Detecting A Double-click On A Youtube Url With Javascript

Solution 1:

Solution 2:

You want:

$('a[^=http://youtube.com]').dblclick(function(e){
    e.preventDefault();
    // Insert video iframe, or whatever you intend to do.
});

You don't want to use just plain JavaScript here, or you'll run into cross browser issues, etc. If you want to do the check manually, rather than trusting jQuery's regex, use:

$('a').dblclick(function(e){
    var pattern = /^http\:\/\/www\.youtube\.com/;
    if(pattern.test($(this).attr('href')))  // Does the href start with http://www.youtube.com/
    {
        e.preventDefault();
        // Insert video iframe, or whatever you intend to do.
    }
});

If you truly insist on not using jQuery, try this:

functiondblclick_handler(el)
{
    var pattern = /^http\:\/\/www\.youtube\.com/;
    if(pattern.test(el.href))  // Does the href start with http://www.youtube.com/
    {
        // Insert video iframe, or whatever you intend to do.returnfalse;
    }
    returntrue;
}

Then:

<ahref="http://www.youtube.com/test/"ondblclick="dblclick_handler(this);">Click me!</a>

Note, you should use onclick here anyways.

Solution 3:

Does this question help?

UPDATE

HTML

<divondblclick="alertDouble()" >Doubleclick me</div>

JS

function alertDouble () {
    alert('Double')
}

But since <a> fires an event anyway off of one click, why not override that one only? You'll run into trouble trying to grab the double.

Solution 4:

Select your element first, for example if your element has the id 'desiredElement', do the following:

document.getElementById('desiredElement').ondblclick = function() {
    alert('action');
};

You can, of course, use other techniques to select the desired element.

Solution 5:

try this:

var a = document.getElementsByTagName('a'),
    total = a.length - 1,
    i;

for ( i = 0; i <= total; i++) {

   a[i].onclick = checkLink;

}


functioncheckLink(e) {

    e.preventDefault();

    var href = e.currentTarget.href;

    if ( href.match(/^http:\/\/youtube\.com/)) {

        //if this validate

    } else {

        window.location = href;

    }
}

Post a Comment for "Detecting A Double-click On A Youtube Url With Javascript"