动态广告code更换页动态、广告、换页、code

2023-09-10 16:33:45 作者:'呆呆小逗比ゞ

我有一个使用移动广告提供商的申请;广告商的工作方式是,我做在服务器端的请求,提供者将返回我的标记和我包括我的网站上。

I have an application that uses a mobile ad provider; the way ad provider works is that I make a request on the server side, the provider returns me the mark-up and I include that on my site.

为了使页面加载速度快(这是一个要求,我不控制),我有一个AJAX调用到我的服务器,然后再打电话给服务器端返回的标记和我有标记在我的移动视图。

In order to make the load page faster (which is a requirement that I don't control), I have an AJAX call to my server which then makes the call to the server side, returns the markup and I include the markup on my mobile view.

大多数时候,我收到的标记是< IMG> 或类似的东西。

Most of the time, the markup I receive is a <img> or something similar.

然而,当我收到的广告标记与&LT;脚本&GT; ,使另一个调用动态接收的广告,我从动态加载&LT;脚本&GT; 结束了更换整个视图;留下我只是广告。

However, when I receive a ad markup with <script> that make another call dynamically to receive the ad, the markup I receive from the dynamically loaded <script> ends up replacing the entire view; leaving with me just the ad.

我曾尝试它周围的一些事情,但都没有成功为止。有什么我可以做确保动态收到的标记没有结束更换整个页面内容?

I have tried a few things around it but haven't been successful so far. Is there anything I can do make sure the dynamically received markup doesn't end up replacing the entire page contents?

推荐答案

这个问题可能是因为广告使用文件撰写,因为它的输出。

The problem is probably because the ad uses document.write as it's output.

文件撰写是硬拼依赖   时序。如果文件撰写被称为   onload事件之前,追加或   插入文本到页面中。如果是   完全名为后onload事件,它   替换页,摧毁了什么   面前。 - crockford.com 的

document.write is recklessly dependent on timing. If document.write is called before the onload event, it appends or inserts text into the page. If it is called after onload, it completely replaces the page, destroying what came before. - crockford.com

你可以做的就是为临时覆盖执行document.write方法。它是如此简单:

What you can do is to temporarily overwrite the document.write method. It is as simple as that:

// overwrite document.write with a custom function
var old = document.write;
document.write = function ( html ) {
  document.getElementById("target").innerHTML += html;
};

// ad code goes here, calling
// our document.write sandbox
document.write("<div> advertisement </div>"); 

// restore standard document.write
document.write = old;

您可以看到一个小的 演示这里

You can see a little demo here.