在DIV和执行放置脚本标记 [英] Placing script tag in div and execute

查看:129
本文介绍了在DIV和执行放置脚本标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个小的广告系统为我的网站,这样我就可以在数据库中添加新的广告。大多数广告包括图像,我只是写一个形象innerHTML来一个div。这工作得很好。 然而,有些广告是由一个脚本标记只应该放在哪里的广告而被通缉。因为我必须从任一code动态地做到这一点的.NET或Ajax调用的背后,我有一些问题与此有关。 只设置一个div的innerHTML的是这样的:

I'm building a small advertisement system for my website so that I can just add new ads in the database. Most ads consists of images and I will just write an image as innerhtml to a div. This works fine. However, some ads consists of a script tag only that should be placed where the ad is wanted. Since I have to do this dynamically from either code behind in .NET or from an ajax call, I have some problems with this. Just setting a innerhtml of a div like this:

<div id="asdf" runat="server">
    <script type="text/javascript" src="xxxxx"></script>
</div>

...是行不通的,因为它不会被执行。不管我在后面的页面加载code设置,或者设置与AJAX调用。

... won't work since it won't get executed. No matter if i set it in the code behind on page load, or set with an ajax call.

我也许能调用的eval()不知何故,但是这并不能保证该脚本将被精确地放置在那里我希望它是,如果我是正确的。

I might be able to call eval() somehow, but this doesn't guarantee the script will be placed exactly where i want it to be if I got it right.

任何想法?这不可能是一个新的问题,许多广告网络有脚本标记,而不是图像和这样的。

Any ideas? This can't be a new problem, many advertising networks has script tags instead of images and such.

编辑: JavaScript文件位于另一个域。一切正常,如果我设置src属性只包含一个警告本地文件!你不能让这种情况发生跨域?

The javascript file is located on another domain. Everything works if i set the src attribute to a local file containing only an alert! Can't you make this happen across domains?

推荐答案

您可以试试这个:

var script = document.createElement('script');
script.src = 'xxxxxx';
script.type = 'text/javascript';
var div = document.getElementById('asdf');
div.appendChild(script);

这篇关于在DIV和执行放置脚本标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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