Doc: improve iframe load speed

This commit is contained in:
陈嘉涵
2017-09-04 13:01:58 +08:00
parent 8a951895f8
commit 225df48ba5
9 changed files with 83 additions and 43 deletions
+36
View File
@@ -0,0 +1,36 @@
/**
* 同步父窗口和 iframe 的 vue-router 状态
*/
import isMobile from './is-mobile';
window.syncPath = function(dir) {
const router = window.vueRouter;
const isInIframe = window !== window.top;
const currentDir = router.history.current.path;
const iframe = document.querySelector('iframe');
if (!isInIframe && !isMobile && iframe) {
iframeReady(iframe, () => {
iframe.contentWindow.changePath(currentDir);
});
}
};
window.changePath = function(path) {
const router = window.vueRouter;
router.replace(path);
};
function iframeReady(iframe, callback) {
const doc = iframe.contentDocument || iframe.contentWindow.document;
if (doc.readyState === 'complete') {
callback();
} else {
iframe.onload = () => {
setTimeout(() => {
callback();
}, 50);
};
}
}