如何安全打开外链接

Coding时出现一个错误:

error  Using target="_blank" without rel="noopener noreferrer" is a security risk: see https://mathiasbynens.github.io/rel-noopener  react/jsx-no-target-blank

原因是:为了防止钓鱼事故,我们需要将外链中的window.opener置为null。

方法一:

a标签设置target="_blank"属性时,额外设置属性rel="noopener norefferrer"

方法二:

使用window.open(url)打开外链时,同时清空新窗口的window.opener和window.location

var otherWindow = window.open();
otherWindow.opener = null;
otherWindow.location = url;

参考:

https://www.jianshu.com/p/c8319e095474
https://mathiasbynens.github.io/rel-noopener/#hax

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容