在页面上,假设test.html
以下代码正在生成 iframe
<iframe src='" + localStorage.getItem(z) + "' class='alink'></iframe>
iframe 中的链接定义为/product-1
/product-2
. 现在由于购物车设置。发生以下情况。
用户来了 site/test.html
一个 iframe 加载 url site/product-1
另一个 iframe 加载 url site/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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句