问题,使用jQuery在iframe中动态显示javascript google广告 [英] problem to display dynamically a javascript google ad in an iframe using jquery

查看:89
本文介绍了问题,使用jQuery在iframe中动态显示javascript google广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们尝试在我们动态添加的iframe中展示Google广告。在iframe中,字段src通常是一个网址,但我们希望使用data:text / html格式来直接使用我们的广告代码。它适用于简单的JavaScript代码,如document.write('hello world')< \ script>,但不适用于谷歌广告代码。
我们在一个html文件中模拟这个:

 < html> 
< head>
< script src =http://code.jquery.com/jquery-latest.min.js>< / script>
< script type =text / javascript>

function resizeFrame(f){
f.style.height = 60 +px;

$ b $(document).ready(function(){;

var htmlCode = document.createElement(div);
var head = document.getElementById('google_ad_468x60');
var myFrame = document.createElement(iframe)
myFrame.name =childframe
myFrame.id =childframe

myFrame.src =data:text / html,+< script>+<! - \\\
g_go_ad_client = \pub-0123456789abcdef\; \\\
g_google_alternate_color = \FFFFFF \; \\\
google_ad_width = 468; \\\
google_ad_height = 60; \\\
google_ad_format = \468x60_as\; \\\
google_ad_channel = \0123456789\; \\\
google_color_border = \ FFFFC6\; \\\
google_color_bg = \FFFFFF \; \\\
google_color_link = \000000\; \\\
google_color_url = \666666\; \\\
google_color_text = \333333\; \\\
// - >+< \ / script>+< script src = \http://pagead2.googlesyndication.com/pagead /show_ads.js\\">\"+\"<\/script>

myFrame.width =468
myFrame.scrolling =no
myFrame.setAttribute('marginheight','0px')
myFrame.setAttribute('marginwidth','0px')
myFrame.setAttribute('frameborder','0')
head。 appendChild(myFrame)

});



< / script>

< / head>

< body onload =resizeFrame(document.getElementById('childframe'))bgcolor =#FFFF00>

< div>
< h1> Google广告前< / h1>
< / div>

< div id =google_ad_468x60>
< - 此处显示Google广告 - !>
< / div>

< div>
< h1>在Google广告< / h1>后
< / div>

< / body>

< / html>

Mozilla没有错误,但使用Chrome我们有这个错误,谷歌广告没有显示:

 不安全的JavaScript尝试从具有URL文件的帧访问具有URL oursitewiththetestfile.com的帧:/// home / lucas / Bureau / google_ad.html从网址数据框中输入:text / html,< script> google_comments< / script> < script type =text / javascriptsrc =google_path.js>< / script> ;.域名,协议和端口必须匹配


解决方案

让黑客和病毒创作者保持联系,他们不允许在与其他域进行通信时使用JavaScript访问iframe,否则将访问托管的网站。

您将被困在此方法中,我不确定是否有解决方法。无论采用哪种方式,Google广告都应直接放入网站,因为它会使用内容生成相关广告以显示在网页上。如果您正在寻找一种方法来显示自己的广告,则应该采用动态方式完成,使用所选的代码语言编写,或使用adsense.google.com上的广告创作者显示自己的广告,而不是公共服务广告。

我个人觉得这样会更简单。

We try to display the google ad in an iframe that we add dynamically. In an iframe, the field "src" is generally an url, but we want to use the data:text/html format to be able to use directly our ad code. It works for simple JavaScript code like a document.write('hello world')<\script>, but not with the google ad code. We simulate this in an html file :

<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script type="text/javascript">

            function resizeFrame(f) {
                f.style.height = 60 + "px";
            }

            $(document).ready(function() {;

                var htmlCode = document.createElement("div"); 
                var head = document.getElementById('google_ad_468x60');
                var myFrame = document.createElement("iframe")
                    myFrame.name      = "childframe"
                    myFrame.id        = "childframe"

                    myFrame.src       = "data:text/html, " + "<script>" + "<!--\ngoogle_ad_client = \"pub-0123456789abcdef\";\ngoogle_alternate_color = \"FFFFFF\";\ngoogle_ad_width = 468;\ngoogle_ad_height = 60;\ngoogle_ad_format = \"468x60_as\";\ngoogle_ad_channel =\"0123456789\";\ngoogle_color_border = \"FFFFC6\";\ngoogle_color_bg = \"FFFFFF\";\ngoogle_color_link = \"000000\";\ngoogle_color_url = \"666666\";\ngoogle_color_text = \"333333\";\n//-->"+"<\/script>"+ "<script src = \"http://pagead2.googlesyndication.com/pagead/show_ads.js\">"+"<\/script>"

                    myFrame.width     = "468"
                    myFrame.scrolling = "no"
                    myFrame.setAttribute('marginheight', '0px')
                    myFrame.setAttribute('marginwidth' , '0px')
                    myFrame.setAttribute('frameborder' , '0'  )
                head.appendChild(myFrame)

            });



        </script>

    </head>

    <body onload="resizeFrame(document.getElementById('childframe'))"  bgcolor="#FFFF00">

        <div>
            <h1>Before Google ad</h1>
        </div>

        <div id="google_ad_468x60">
            <-- Here is display the the Google ad --!>
        </div>

        <div>
            <h1>After Google ad</h1>
        </div>

    </body>

</html>

We do not have an error with Mozilla, but with Chrome we have this error, in both case the google ad is not displayed:

Unsafe JavaScript attempt to access frame with URL oursitewiththetestfile.com from frame with URL  file:///home/lucas/Bureau/google_ad.html from frame with URL data:text/html, <script>google_comments</script> <script type="text/javascript" src="google_path.js"></script>. Domains, protocols and ports must match

解决方案

In Chrome's intent to keep hackers and virus creators at bay, they do not allow access to an iframe using javascript when it is communicating with a different domain then the site it is hosted on.

You are going to be stuck in this method and I am unsure that there is a workaround. Either way, Google ads should be placed directly into the website as it uses the content to generate relevant advertisements to be displayed on the page. If you are looking for a way to display your own advertisements, it should either be done dynamically, written in the code language of choice, or using the ad creator on adsense.google.com to display your own ads instead of public service ads.

I personally feel that it would be much more simple that way.

这篇关于问题,使用jQuery在iframe中动态显示javascript google广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆