如何在 JavaScript 中等待 location.href 完成?

发布于 2024-08-11 02:42:36 字数 1069 浏览 2 评论 0原文

下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┼── 2024-08-18 02:42:36

我不相信用 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:

  • Load the external file in a popup or a new window. I haven't tried that so I'm not sure how it will work in all the browsers.
  • Use whatever server side language you are using to serve up the file. You will then have the ability to redirect the client after the file has been served.
半枫 2024-08-18 02:42:36

正如其他人所说,Javascript 没有可用的“onDownloadComplete”事件。但是,如果您愿意在下载时显示不同的内容,则可以使用以下技术:

  1. 在页面 A 上,在页面 B 上有指向页面 B 的链接
  2. ,显示您想要的内容要在文件下载时向用户显示
  3. 在页面 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:

  1. on page A, have a link to page B
  2. on page B, display the content that you want to show users while the file is downloading
  3. inside the element for page B, add a META tag that actually starts the download, i.e.,

    <head>
        <meta http-equiv="refresh" content="1;url=http://server.com/document.doc" />
    </head>
    

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.

残月升风 2024-08-18 02:42:36

据我所知,您有两个选择

  • 在弹出窗口中链接到文件(如果浏览器找到的唯一内容是可下载文件,则通常应该自行关闭此窗口),同时重定向到目标页面
  • 构建一些动态链接到目标页面并在那里启动下载的函数,但这在您的情况下可能没有用

纯 JavaScript 这是不可能的,并且通过服务器端脚本进行隧道下载也无法处理重定向。恐怕你必须在两者之间找到一个解决方案

You have two options as far as i can tell

  • Link to the file in a popup window (browsers should normally close this themselves if the only thing they find is a downloadable file) and at the same time redirect to the destination page
  • Build in some dynamic function that links to the destination page and initiates the download there but that might not be usefull in your case

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文