Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to inject 3rd party image tags into container iframe after page load?

Posted on 2016-11-03
5
70 Views
Last Modified: 2016-11-04
I'm trying to fire of static marketing pixels in an iframe based on a condition, after the page has loaded but I can't get it to work? I get the following error on line 12:  Uncaught TypeError: Cannot read property 'parentNode' of undefined
var sTags = ['<img src="https://ad.doubleclick.net/ddm/activity/src=4587700;type=oao;cat=im_oaoc;u11=[URL];u12=[New vs Returning];ord=[Random Number]?" />',
             '<img src="https://ad.doubleclick.net/ddm/activity/src=4587707;type=oao;cat=im_oaoc;u11=[URL];u12=[New vs Returning];ord=[Random Number]?" />', 
             '<img src="https://ad.doubleclick.net/ddm/activity/src=4587777;type=oao;cat=im_oaoc;u11=[URL];u12=[New vs Returning];ord=[Random Number]?" />']           
for(var i=0; i<sTags.length; i++) {
    var iframeTag = document.createElement("iframe");
    iframeTag.setAttribute("width", "1");
    iframeTag.setAttribute("height", "1");
    iframeTag.style.display = "none";
    iframeTag.style.border = "none";
    iframeTag.src = sTags[i];
    var ref = document.getElementsByTagName("img")[0];
    ref.parentNode.insertBefore(iframeTag, ref);
}

Open in new window

So not sure what I'm doing wrong or how I accomplish this?

Thanks!
0
Comment
Question by:894359
5 Comments
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41873437
You're setting the src attribute of the new iframe to an <img> tag? The src of an iframe should be a URL.

Do you have any <img> tags on the page? document.getElementsByTagName("img")[0] looks for the first <img> tag on the page. Afterward it will insert the new iFrame with the questionable src attribute in front of it. If there aren't any <img> tags on the page, it will be empty. Hence the error "Cannot read property 'parentNote' of undefined."
0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41873462
I'm trying to fire of static marketing pixels in an iframe based on a condition
Why do you want to do this in an iframe? What advantage does that have over a div on the page?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 41873710
here how to put three images in an iframe and add this iframe to current document at page load:
<script>
    window.onload = function() {
        var sTags = ['http://path/to/img/1','http://path/to/img/2','http://path/to/img/3'];

        var iframeTag = document.createElement("iframe");
        iframeTag.setAttribute("width", "1");
        iframeTag.setAttribute("height", "1");
        iframeTag.style.display = "none";
        iframeTag.style.border = "none";

        for(var i=0; i<sTags.length; i++) {
            var imgTag = document.createElement("img");
            imgTag.src = sTags[i];
            iframeTag.appendChild(imgTag);
        }

        document.body.appendChild(iframeTag);    
    }
</script>

Open in new window

0
 

Author Comment

by:894359
ID: 41874387
Hi Leakim,
I'll try your solution but it must work after the page loads, so I'll try and see .

Thanks,
MJ

Julian,
I thought it would be more secure using an iframe?

Thanks,
MJ
0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41874481
I thought it would be more secure using an iframe?
Secure against what exactly?
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question