indora_no_ya
indora_no_ya

Reputation: 65

Can I use a socket through multiple HTML pages?

For example, if a user enters my webapp through the standard index.html page that is loaded automatically, can that socket still be used if the user clicks a button on my webpage that loads another html page thats part of my webapp? Right now, I have one front end js page that is used for my 3 html files. It works fine for my standard index file, but when I click a button that leads to another html file (Which is also connected to my front end js page), it says that io is not defined in the console.

Upvotes: 0

Views: 232

Answers (1)

Kryten
Kryten

Reputation: 15780

Loading a new HTML page will cause the Javascript on the new page to execute - this will destroy any context from the previous page (including established connections).

There are a couple of ways around this:

  1. write your code to re-establish the connection on page load. This will require passing an identifier of some sort so the new connection is associated with the existing session.

  2. write your application as a single page app (SPA). In an SPA, the pages are rendered on the client side, as part of a Javascript app passed to the client on the first load. Thus, since there is no reload when a user is moving from one page to another, the connection is not lost.

Upvotes: 1

Related Questions