how can i pass a variable from an EJS page to another EJS page.
For example, I have this page
// insertion.ejs // <!DOCTYPE html> <html lang=”en”> <head></head> <header> <% include ../views/header %> </header> <body> <script> var a="hello"; </script> </body> </html>
And this page
// get.ejs // <!DOCTYPE html> <html lang=”en”> <head></head> <header> <% include ../views/header %> </header> <body> <script> var b </script> </body> </html>
So how can I pass the content of the variable a to the variable b
If you mean to pass the value when a user clicks a link and goes from one page to the other, then you can use a query parameter and have a link like:
name is the name of the query parameter and
value is its value, that could be populated by the EJS template with an appropriate data.
If you want to pass the variable in a sense that you want to have that variable available to a user then you need to use some session storage, a cookie or local storage.
If you want to pass the variable from one page to the other all users just to reduce duplication then you should use
include to include that in all pages that need it or to pass it by the handlers.
This is not very clear from your question what is your intention but this is more or less what you can do in all scenarios.
You can use LocalStorage concept. Here is the explanation how to use local storage. https://www.w3schools.com/html/html5_webstorage.asp
Well you can model binding in one page, but not two separate page. For this purpose you have cook up your own recipe, you have two novel ways of doing this, first use socket.io , with this method let each page publish and subscribe to events, whenever one page emits a event, the other automatically receives it and vice versa. The alternative approach is to utilize node local storage and cache system modules. For local storage you can memcache, node-cache just do a npm search. For a fast and direct caching use redis,redis in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries.
Happy Coding :):