Advanced Search

Results 1 to 5 of 5

Thread: Intercept <img> and <iframe> tag loads

  1. #1
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Intercept <img> and <iframe> tag loads

    Hi,

    I need to block certain image and iframe content requests to third party urls, based on a condition. Is there a way by which I can intercept the element loads to prevent the HTTP requests being sent?

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,629
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    is this your webpage? or are you asking from a user's perspective?

    former:
    don't write the offending <img> or <iframe> elements to the page.
    If something prevents you from doing this, please explain your situation in more detail.

    latter:
    most modern browsers can be configured to block content from specific domains (to block the iframe you'd probably need to block the domain it serves).
    Check your browser's documentation.
    Edit:

    I'm sorry; I missed the "based on a condition" part of your post.

    If you want to decide whether or not to have an image/iframe, simply leave it out to start, and then write it to the document (or not) via javascript:
    Code:
    var show_iFrame = /* true or false based on whatever condition */;
    
    if( show_iFrame ){
        var iFrame = document.createElement( 'iframe' );
        var iFrame_src = document.createAttribute( 'src' );
        iFrame_src.nodeValue = 'http://custom-anything.com';
        iFrame.setAttributeNode( iFrame_src );
        var body = document.getElementsByTagName( 'body' )[0];
        body.appendChild( iFrame );
    }
    I'm "not that great" with javascript, but the above works (though it could probably be done better).

    Last edited by traq; 04-29-2012 at 04:46 PM.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  3. #3
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi,

    The <img> and <iframe> tags are present in my website, but there are N number of them, so it is practically difficult to modify each of them. What I am trying to achieve is to write a central script that will be included in the head of each of these pages (All of them share a common header). I am trying to intercept the element loads from this script.

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,629
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I don't know that that approach would work (not sure it's actually possible). Maybe give all the elements in question display: none;, and then JavaScript could try to rewrite the page on the fly...

    It would be much more difficult, in any case. I would seriously consider taking the time to rewrite the HTML.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  5. #5
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    I would consider minimising the number of iframes first of all. Use SSI includes, PHP includes or ASP.NET Master Pages for templates and menus etc.

    Then use the iframe's onload event, or HTML5's onready event to capture the iframe's source. Remember to grab the iframe.contentWindow.location.href rather than the iframe.src.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •