Link to home
Start Free TrialLog in
Avatar of MJ
MJFlag for United States of America

asked on

Removing iframe elements

I have the following code for injecting DoubleClick tags on a page:
// DOM Injection routines
            var iframeTag = document.createElement("iframe");
            iframeTag.setAttribute("width", "1");
            iframeTag.setAttribute("height", "1");
            iframeTag.style.display = "none";
            iframeTag.style.border = "none";
            iframeTag.src = dcUrlBuilder("//fls.doubleclick.net/activityi", src, type, cat, "1");
            var ref = document.getElementsByTagName("script")[0];
            ref.parentNode.insertBefore(iframeTag, ref);

Open in new window

It dynamically creates DoubleClick Tags and injects them into the page using an iFrame. I'm now working with a MVC framework so what I now need to do is remove every tag that was created on the previous view. I have no idea how to approach this? Is there a way to do this?
Avatar of Rob
Rob
Flag of Australia image

Bear with me while I work out what you're trying to do...
When you load a view you're loading a completely new page so I'm not understanding what you mean by removing the tags from the previous view.  
My understanding is when you load the new view, the previous view is replaced.
What mvc framework ate you using?
Avatar of MJ

ASKER

Sorry, this is hard to explain. Yes, a new view is loaded but there is no http call therefore the iFrame still exists.... wouldn't it? That's what I'm assuming. The MVC framework an ASP.net framework. I'm loading the DoubleClick tags via Adobe DTM.
Ok I'm out if my depth with .net stuff. I use php based mvc. I have heard about partial post backs but not sure if that's related. sorry. I'll get another expert involved
I, too, don't quite get what you are trying to achieve.  Are you trying to figure out how to remove an element from the DOM from a different HTML page?
Avatar of MJ

ASKER

Hopefully this helps explain a little more? I have double click tags that are dynamically loaded based on the current view. So when View1 renders I want to load doubleclick tags  A & B. Then when a new View2 renders I want to load doubleclick tags  C, D, E & F. The issue is, Tags A & B will also still be there so View2 will have tags A-F instead of C,D, E &F. I need a way to eliminate/unload Tags A & B.
Are tags A & B in different iframes from tags C, D, E & F?  You would need to be able grab a reference to the tag, and then remove it from its parent.
Avatar of MJ

ASKER

The tags are in the same iFrame.
Avatar of MJ

ASKER

I was wondering if I name the iFrame, can I close it before recreating?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MJ

ASKER

Here's what worked for me. I just gave the iFrame and ID and then closed it using the ID.

var iframeTag = document.createElement("iframe");
            iframeTag.setAttribute("width", "500");
            iframeTag.setAttribute("height", "500");
         iframeTag.setAttribute("id", "dc_target");
            iframeTag.style.display = "none";
            iframeTag.style.border = "none";
            iframeTag.src = dcUrlBuilder("//fls.doubleclick.net/activityi", src, type, cat, "1");
          var ref = document.getElementsByTagName("script")[0];
            ref.parentNode.insertBefore(iframeTag, ref);

and then do this:

 var frame = document.getElementById("dc_target");
            frame.parentNode.removeChild(frame);