在一个页面上,假设test.html
以下代码正在生成iframe的
<iframe src='" + localStorage.getItem(z) + "' class='alink'></iframe>
iframe 中的链接定义为/product-1
/product-2
。现在由于购物车设置。将发生以下情况。
用户上site/test.html
一个 iframe 加载 URLsite/product-1
另一个 iframe 加载 URLsite/product-2
产品现在都已添加到购物车,但购物车会自动重定向回test.html
然后再次加载 iframe 并将产品添加到购物车。所以这是一个循环,它向购物车中添加了太多的产品;-)
我无法更改重定向行为,因为它会破坏客户习惯于使用网站的方式。所以我想一定有一种方法可以嵌套iframe。
那么我如何只允许一个 iframe 并禁止 iframe 进入另一个 iframe。
// RETRIEVER
(function ($) {
$(document).ready(function(){
let dummy = Object.keys(localStorage).filter(v => v.startsWith("LinkOrderURL"));
dummy.forEach(z => $("body").append("<iframe src='" + localStorage.getItem(z) + "' class='alink'></iframe>"));
});
})(jQuery);
最终,我使用此代码来封装上述内容,并使其工作。
if ( self !== top ) {
// you're in an iframe
} else {
// you're not in an iframe
}