back-download.js 1.07 KB
// 微信返回注册下载事件
function pushHistory() {
    var pages = ['yh_download', 'yh_forward'];
    var state = {
        title: '下载页面',
        page: pages[0]
    };
    var url = location.href;

    if (location.hostname === 'activity.yoho.cn') {
        state.url = '//activity.yoho.cn/api/wechat/download';
    } else if (location.hostname === 'm.yohobuy.com') {
        state.url = '//m.yohobuy.com/api/wechat/download';
    } else {
        return false;
    }

    window.addEventListener('popstate', function(e) {
        var temp = e.state;

        if (temp && pages.indexOf(temp.page) > -1) {
            location.reload();
        }
    });

    if (!window.history.pushState || window.history.length > 1) {
        return false;
    }

    try {
        window.history.replaceState(state, null, state.url);
        window.history.pushState({page: pages[1]}, null, url);
    } catch (e) {
        return;
    }
}

if (/micromessenger/i.test(navigator.userAgent) &&
    document.getElementById('main-wrap').getAttribute('data-backDownload')) {
    pushHistory();
}