如何在 JavaScript 中等待 location.href 完成?
下面的 JavaScript 代码有两个步骤。
第 1 步:转到 .pdf、.doc、.exe 或非 html 原生的文件。如果 location.href 已占据浏览器窗口,则无需执行步骤 2。(PDF 通常占据浏览器窗口)。大多数其他事情都会启动下载管理器进程。比如.exe。但有些内容(例如 Word 文档)可以下载或直接显示在浏览器窗口中,具体取决于浏览器设置。我希望它做 hef.location 让它做的事情。
第 2 步:但是,如果在该过程完成后正在下载 .exe 等文件,则转到主页。
或者在步骤 1 和步骤 2 之间等待 5 秒的解决方案似乎在大多数情况下都有效。但在较慢的连接上,它并不总是有效。然后它会在没有完成第一个 href.location 调用的情况下进入主页,并且他们永远不会看到 PDF,而只会看到主页。
仅供参考...我将它们包装在 setTimeOut 中的原因与此 Firefox 问题有关。 堆栈溢出:864633 分配到-document-location-href-without- clobbering-history
我的问题:有没有办法知道 location.href 进程何时完成?
<script language="JavaScript"><!--
function windowOnLoad() {
setTimeout(function(){
location.href='/someurl/something.pdf'; //sometimes this is .doc file
},0);
setTimeout(function(){
location.href='/homepage';
},5000);
return false;
}
//-->
</script>
The JavaScript below code has two steps.
Step 1: go to .pdf, .doc, .exe or something which not html native. If location.href has took over the browser window then there is no need to do step 2. (PDFs usually take over browser window). Most other things kick off a download manager process. Such as .exe. But there are some things such as word docs that could be either a download or show right in the browser window depending on browser setup. I want it to do what hef.location would make it do.
Step 2: But if it is downloading a file such as an .exe after that process is done then go to home page.
Or solution for just waiting 5 seconds between steps 1 and 2 seem to work most of the time. But on a slower connection it doesn't always works. Then it goes to home page without finishing the first href.location call and they never see the PDF and just see home page.
FYI...The reason I am wrapping them in setTimeOut is related to this firefox issue. Stack Overflow: 864633 assigning-to-document-location-href-without-clobbering-history
My question: Is there a way to tell when the location.href process gets completed?
<script language="JavaScript"><!--
function windowOnLoad() {
setTimeout(function(){
location.href='/someurl/something.pdf'; //sometimes this is .doc file
},0);
setTimeout(function(){
location.href='/homepage';
},5000);
return false;
}
//-->
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不相信用 JavaScript 可以做到你所要求的。当您为 window.location 设置某些内容时,您基本上是在告诉浏览器在当前窗口中加载指定的 url。当浏览器执行此操作时,前一页上的所有代码都将停止运行。
我想到了以下几种可能性:
I don't believe it is possible to do what you are asking in JavaScript. When you set something to window.location you are basically telling the browser to load the the specified url in the current window. When the browser does that all the code on the previous page ceases to operate.
Here are a couple possibilities from the top of my head:
正如其他人所说,Javascript 没有可用的“onDownloadComplete”事件。但是,如果您愿意在下载时显示不同的内容,则可以使用以下技术:
在页面 B 的元素内,添加实际开始下载的 META 标记,即
<前><代码><头>
最终的结果是浏览器将继续显示(2)中的内容,同时也会提示用户通过“另存为...”对话框保存下载。
许多下载网站(例如 CNet 和 Sourceforge)都使用类似的技术。
As others have stated, there is no 'onDownloadComplete' event available to Javascript. However, if you are amenable to displaying a different while the download is occurring, you can use the following technique:
inside the element for page B, add a META tag that actually starts the download, i.e.,
The end result is that the browser will continue to show the content from (2), but also prompt the user to save the download via the 'Save as...' dialog box.
Many download sites like CNet and Sourceforge use a similar technique.
据我所知,您有两个选择
纯 JavaScript 这是不可能的,并且通过服务器端脚本进行隧道下载也无法处理重定向。恐怕你必须在两者之间找到一个解决方案
You have two options as far as i can tell
Pure javascript this is impossible and tunneling a download through a serverside script can't handle redirects either. You'll have to go with a solution inbetween i'm afraid