在JavaScript中,如果你想要重定向到一个空白窗口并在加载之前预加载同一域下的整个URL,可以使用以下方法:
你可以使用window.open
创建一个空白窗口,并使用fetch
API预加载页面内容。以下是一个示例代码:
function preloadPage(url) {
// 创建一个空白窗口
const newWindow = window.open('', '_blank');
// 使用fetch API预加载页面内容
fetch(url)
.then(response => response.text())
.then(html => {
// 将预加载的内容设置到新窗口的document中
newWindow.document.write(html);
newWindow.document.close();
})
.catch(error => {
console.error('预加载失败:', error);
});
}
// 示例:预加载同一域下的页面
const targetUrl = 'https://example.com/page-to-preload';
preloadPage(targetUrl);
fetch
请求成功,并且正确使用document.write
和document.close
方法。通过上述方法,你可以在JavaScript中实现重定向到空白窗口并在加载之前预加载同一域下的整个URL。
领取专属 10元无门槛券
手把手带您无忧上云