页面被iframe与无刷新更换url方法

  1. 页面被iframe问题解决方法

     if (window.top.location !== window.self.location) {
             const data = JSON.stringify({ iframedRedirectSuccess: true });
             window.top.location.href = addURLParam(window.self.location.href, 'data', data);
           }
  2. 页面初始化获取参数

     import URL from 'url-parse';
     const {
         origin, protocol, host, hostname, port, query, hash,
       } = new URL(document.URL, true);
    
       const hostInfo = {
         origin, protocol, host, hostname, port, query, hash,
       };
  3. 页面无刷新删除url上参数(仅适用于无跨域更换url及参数)

      import qs from 'querystringify';
      import omit from 'lodash/omit';
      const param = omit(qs.parse(window.location.search), ['data']);
         const paramStr = qs.stringify(param, '?');
         const url = `${window.location.pathname}${paramStr}`;
         // This is for firefox will reload previous url when we click refresh button or press F5
         // after we use replaceState to replace a new url issue.
         window.location.hash = window.location.hash; // eslint-disable-line 
         window.history.replaceState && window.history.replaceState({}, null, url);

猜你喜欢

转载自www.cnblogs.com/weilantiankong/p/11435976.html