带Href的On-Click A-tag,套接字断开连接 [英] On-Click A-tag with Href, socket gets disconnected
问题描述
我正在使用socket.io处理一个项目。我想给用户一些链接以下载文件:
< a href =< path> >连结名称< / a>
当我点击链接下载文件时,我的套接字被断开。
当我使用这个
< a href =< ;路径>中target =_ blank>链接名称< / a>
工作正常。出现这种情况的原因是什么?
当您在同一个窗口中关注链接时,当前页面的环境完全被拆除,包括您的代码(以及socket.io的代码)在其中运行的整个JavaScript环境。这就是为什么当你在当前窗口中单击一个到新页面的链接时这样做的原因,但是当你打开一个新窗口时( target =_ blank
), p>
你会想看看各种单页面应用技术,主要涉及到使用ajax将内容交换到当前页面(并更新散列以便URL不同)而无需将全新的页面加载到窗口中。
I am working on a project using socket.io. I want to give user some links to download a file as
<a href="<path>" >Link Name</a>
When I click on link to download file, my socket gets disconnected.
When I use this
<a href="<path>" target="_blank">Link Name</a>
it works fine. Any reason why this happens?
When you follow a link within the same window, the current page's environment gets completely torn down, including the entire JavaScript environment in which your code (and socket.io's code) is running. That's why it does this when you click a link to a new page within the current window, but not when you open a new window (target="_blank"
).
You'll want to look at the various single-page-application techniques, which mostly involve swapping content into the current page using ajax (and updating the hash so the URL is different) without loading an entirely new page into the window.
这篇关于带Href的On-Click A-tag,套接字断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!