我的问题是我想添加JavaScript到HTML页面,感谢安迪E,我找到了方式add my JavaScript into a html page and be executed。但现在我的问题是,所有的页面被JavaScript覆盖。有人知道如何做同样的事情,但没有用JavaScript覆盖页面,要添加到代码或东西来改变:)?动态添加JavaScript覆盖HTML页面视图(不是代码)
- 我不想重新加载页面,因为我不希望用户看到页面的两倍,如果页面包含了很多东西,这将是非常不方便的事情。
这是一个短代码,告诉你我的问题:(我改变了广告代码,因为它不是我的,所以我不能把它放在这个公共场所)
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {;
var correct_div = document.getElementById('ad_468x60');
var sTag1 = document.createElement("script");
sTag1.type = "text/javascript";
sTag1.text = "<!--\ngoogle_ad_client = \"pub-123456789\";\ngoogle_alternate_color = \"FFFFFF\";\ngoogle_ad_width = 468;\ngoogle_ad_height = 60;\ngoogle_ad_format = \"468x60_as\";\ngoogle_ad_channel =\"123456789\";\ngoogle_color_border = \"FFFFC6\";\ngoogle_color_bg = \"FFFFFF\";\ngoogle_color_link = \"000000\";\ngoogle_color_url = \"666666\";\ngoogle_color_text = \"333333\";\n//-->"
correct_div.appendChild(sTag1);
var sTag2 = document.createElement("script");
sTag2.type = "text/javascript";
sTag2.src = "http://google_ad_url.js";
correct_div.appendChild(sTag2)
});
</script>
</head>
<body>
<div>
"This text appears and disappears after the ad is displayed"
</div>
<div id="ad_468x60">
<!-- The JavaScript come here -->
</div>
</body>
</html>
编辑:我可以使用iframe来做到这一点吗?
好吧,安迪,再次感谢你的所有答案。谷歌广告似乎本身在iframe中,你认为这会是一个问题吗?我们正在尝试第一个解决方案 – Lucas 2010-08-09 17:24:59
@Lucas:这不应该是一个问题。如果* document.write()*只是将一个iframe元素写入页面,那么使用重写方法可能会更容易一些。 – 2010-08-09 17:27:05
似乎它不写入元素,但这里是谷歌代码,它会更简单: http://pagead2.googlesyndication.com/pagead/show_ads.js 所以我尝试重写方法 – Lucas 2010-08-09 19:03:28