Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On-Click A-tag with Href, socket gets disconnected

Tags:

html

href

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?

like image 870
Harpreet Singh Avatar asked Oct 17 '25 22:10

Harpreet Singh


1 Answers

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.

like image 88
T.J. Crowder Avatar answered Oct 21 '25 23:10

T.J. Crowder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!