Results 1 to 4 of 4

Thread: How do I get this function to work in IE?

  1. #1
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question How do I get this function to work in IE?

    Hi there,

    I'm trying to make a javaScript function that can alter a piece of HTML code based on whether an ad-blocker is detected.

    I want to to modify the top margin of an iframe. To do so, I need to execute a function ( writeiframe() ) inside another function that is inside setTimeout(). This works well in Chrome, but for some reason I can't get it to work in IE.

    I have posted the main javaScript code below. This is a link to the full code: http://kulsvierhytten.dk/iframe-adblock.html

    I'm not quite used to dealing with javaScript, so any and all help will be greatly appreciated.

    Code:
    	function detect()
            {
                //create a iframe. Append the iframe to the body. And then after 100ms check if their offsetHeight, display or visibility is set such a way that user cannot see them.
                //In the URL use the words specific to advertising so that Adblock can do string matching.
                var iframe = document.createElement("iframe");
                iframe.height = "1px";
                iframe.width = "1px";
                iframe.id = "ads-text-iframe";
                iframe.src = "http://domain.com/ads.html";
                
                document.body.appendChild(iframe);
                
                setTimeout(function()
                           {
                           	   var iframe = document.getElementById("ads-text-iframe");
                               if(iframe.style.display == "none" || iframe.style.display == "hidden" || iframe.style.visibility == "hidden" || iframe.offsetHeight == 0)
                               {
                               		adBlock = "margin-top: -174px;";
    								alert(adBlock);
    								iframe.remove();
    								writeiframe();
    							}
                               else
                               {
                               		adBlock = "margin-top: 0px;";
    								alert(adBlock);
                               		iframe.remove();
    								writeiframe();
                               }
                           }, 100);
            }
    detect();
    Last edited by Beverleyh; 05-29-2015 at 11:54 PM. Reason: Formatting

  2. #2
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,296
    Thanks
    1
    Thanked 240 Times in 235 Posts

    Default

    Hi there christianbarkholt,

    and a warm welcome to these forums.

    Your problem is caused by the use of "remove()".

    Further reading:-


    Instead of...
    Code:
    
    iframe.remove();
    ...it would be safer to use...
    Code:
    
    document.body.removeChild(iframe);
    Also note that you really should not be using "document.write()"
    here either.

    But if you must insist on using it, then you should, at least, include
    "document.close()" at the end of each statement.

    This will stop the page from hanging,

    coothead
    ~ the original bald headed old fart ~

  3. #3
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Wow. Thanks a lot coothead!

    I changed iframe.remove() and added document.close(), and it works perfectly.

    And thanks for the warm welcome too.

    In terms of document.write(): I know it's not really the way to do it. I made the original script a while ago, and will change it when I get some extra time on my hands.

  4. #4
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,296
    Thanks
    1
    Thanked 240 Times in 235 Posts

    Default


    No problem, you're very welcome.

    coothead
    ~ the original bald headed old fart ~

Similar Threads

  1. Hide function doesn't work to IE..
    By ampera in forum JavaScript
    Replies: 2
    Last Post: 01-11-2010, 03:46 AM
  2. How does copy() function work?
    By psikolog in forum PHP
    Replies: 15
    Last Post: 09-03-2009, 08:28 AM
  3. header function don't work immediately?
    By writeman in forum PHP
    Replies: 3
    Last Post: 03-29-2007, 06:16 AM
  4. onLoad function didn't work on Firefox
    By novarli in forum JavaScript
    Replies: 3
    Last Post: 08-29-2006, 12:40 PM
  5. Replies: 0
    Last Post: 01-07-2005, 07:51 PM

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
  •