我正在尝试使用一段代码来解决不允许将网站嵌入为iframe的权限。这段代码本身可以很好地工作,但是当我添加我的代码以从firebase中提取一些文本时它就会停止工作。但是,当iframe代码不存在时,firebase代码也可以正常工作。我需要这两段代码才能在我的页面中工作。
JS :
//firebase code
var ref = new Firebase("https://glowing-torch-9634.firebaseio.com/");
ref.once("value", function(snapshot) {
var data = snapshot.val();
document.write('<span style="background:black; width:100%; height:100px; position:absolute; left: 0px; top: 0px;"><span class="couponContainer" style="background: black; color:#fff; position: absolute; top: 20px; left:20px; text-align: left">' + data.levels[0].tenth.coupon + '</span><span class="backContainer" style="text-align: right;background: black; color:#fff; position: absolute; top: 20px; right:20px;">back</span></span>');
});
//iframe workaround code
var iframe = document.getElementsByTagName('iframe')[0];
var url = iframe.src;
var getData = function (data) {
if (data && data.query && data.query.results && data.query.results.resources && data.query.results.resources.content && data.query.results.resources.status == 200) loadHTML(data.query.results.resources.content);
else if (data && data.error && data.error.description) loadHTML(data.error.description);
else loadHTML('Error: Cannot load ' + url);
};
var loadURL = function (src) {
url = src;
var script = document.createElement('script');
script.src = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20data.headers%20where%20url%3D%22' + encodeURIComponent(url) + '%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=getData';
document.body.appendChild(script);
};
var loadHTML = function (html) {
iframe.src = 'about:blank';
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(html.replace(/<head>/i, '<head><base href="' + url + '"><scr' + 'ipt>document.addEventListener("click", function(e) { if(e.target && e.target.nodeName == "A") { e.preventDefault(); parent.loadURL(e.target.href); } });</scr' + 'ipt>'));
iframe.contentWindow.document.close();
}
loadURL(iframe.src);
然后在 html 正文中我只有一个iframe:
<iframe src="http://www.enemytofashion.com/collections/"></iframe>