Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing links in an iframe

Tags:

jquery

I'm trying to achieve exactly what you've done in this thread: Remove anchor links / form targets within iframe content

I've tried wrapping the jQuery in a function like this:

$('#ifr').load(function(){
    $('#ifr').contents().find('a').click(function() { return false; });
}

But that isn't working. Do you have any tips or suggestions?

like image 320
acb_tld Avatar asked Oct 22 '22 08:10

acb_tld


1 Answers

You have to bind the event when the iframe has loaded. And use e.preventDefault() to stop the default behavior.

Example on jsFiddle

$("iframe").load(function() {
    // note the use o .each, you need to bind all elements
    $(this).contents().find("a").each(function() {
        $(this).click(function(e){e.preventDefault(); alert("click blocked")});
    });
});

The iframe must be from the same origin.

like image 88
BrunoLM Avatar answered Nov 03 '22 17:11

BrunoLM