的Javascript无法删除DIV
问题描述:
其实我提供一个iframe
从我的网站,完美的作品其他用户,但我想如果iframe
代码一些其他网站上使用从而除去特定DIV,我只允许我的网站在iframe
内显示特定的Div。的Javascript无法删除DIV
我已经写了一个代码,可以在localhost/xampp服务器上完美工作,但是这个代码在live网站上不起作用我不知道我在做什么错误吗?
这里是我的实际代码
<script type="text/javascript">
var frameLocation = window.location.hostname;
var whiteLocation = "<?php echo $ShowMyDomain; // prints mywebsite.com ?>";
if (whiteLocation != frameLocation) {
$('#adprimary').remove();
}
</script>
答
您的代码将无法工作,因为变量frameLocation
得到你自己的域名,并配以可变whiteLocation
。要解决此问题,您必须使用document.referrer
函数可能会在其他一些变量中,然后从中提取域名并在frameLocation
中重复使用。
你可以试试这个工作jsFiddle
或这里你有任何控制台的错误,将解决问题
<script type="text/javascript">
$(document).ready(function(){
var urlGet = document.referrer;
var frameLocation = urlGet.match(/:\/\/(.[^/]+)/)[1];
var whiteLocation = "<?php echo $ShowMyDomain; // prints mywebsite.com ?>";
if (whiteLocation!=frameLocation) {
$('#adprimary').remove();
}
});
</script>
的代码?您是否在正式网站上正确链接了jQuery? – Andy
您对iFrame的内容没有任何影响 – Johannes
@Andy JQuery完美工作,在控制台中没有任何错误,因为我说它在localhost上工作,但不在活动网站上工作 – Rtra