window.opener 操作导致的跨域问题
- 背景: 网站埋点统计需要界面来源关系绘制桑葚图分析用户行为,新打开窗口在路由守卫拿不到from,于是使用了 window.opener;导致从外部网站跳转进来就直接报跨域了;
router.afterEach((to, from) => {
let fromObj = { path: from.path, title: from.meta.title, productType: from.meta && from.meta.productType }
if (!fromObj.title && fromObj.path === '/') {
if (window.opener && window.opener.document) {
const title = window.opener.document.title
const path = window.opener.document.location && window.opener.document.location.pathname
fromObj.path = path
fromObj.title = title
}
}
session('frompath', fromObj)
try {
if (window.opener && window.opener.document) {
const title = window.opener.document.title
const path = window.opener.document.location && window.opener.document.location.pathname
fromObj.path = path
fromObj.title = title
}
} catch (e) {
fromObj.path = '/'
fromObj.title = '首页'
}