I am using an external javascript file in two pages. I change the value of a variable from the first page. Now when I use the second page, the value of that variable goes back to its initialisation value. I want to maintain the value of the variable. How can I do this?
var mail="not yet added";
I change this value in page 1 to "xxxxxx#xxxx.com".
Now when I access this in page 2 I get "not yet added"
You can make use of the windows.name object as long as you're in the same windows or tab. Have look here.
Otherwise the persists.js framework offers a number of different ways to persist information and share it between pages etc.
Your final option is of course using a basic cookie.
Write it to a cookie on javascript, otherwise post the value back to the server (i.e. php or whatever you are using) - then you can print the variable explicitly on the next JS page if thats easier.
You can also pass variable to second page by adding #hash to link then in second page just retrieve it and update the variable with hash value.
Related
For example the webpage : www.test.com/a123 has a var called Value that stores quantity of products
the webpage www.test.com/b123 has the same variable also.
On the console of each page, I can call the variable and get their values, but there is some way to get this values without being on that specific page?
I need to get the value table from this pages having only to specify which page I would like.
Something like :
document.getElementByClassName('tamanho__item_pdpjs-tamanho__item_pdp')[5]
But for variables referencing from which page to get
Use the localStorage property of JS.
localStorage.setItem("a123", "value");
// Retrieve on b123 page
document.getElementById("result").innerHTML = localStorage.getItem("a123");
It sounds like you might be struggling to understand where state should live in your application.
The example you mentioned in the comments, Nike and the "SKUsSizes" is likely getting that data from a backend API. Not by accessing a variable on another "web page".
This would be easier to explain if you had a more concrete example but say you need the page b123 to know how many products there are for a123.
The b123 page would make an HTTP request to your backend API requesting details about a123 which may include the number of products.
This data would be stored in a database.
On the other hand, if the product count is coming from some user action that happens over on a123 and you want to see that state change on page b123 then using local storage as suggested in another answer might be one way to solve that problem.
Although it's probably a better idea to use a framework like React, Vue or Angular and use the state management options they have instead of trying to figure out how to do this from scratch.
On my homepage I have to set a cookie using the name of the logged in user. The cookie set and get part has to be done in JS. On the subsequent sub pages I have to retrieve the cookie(username) using the set variable name.
How can I store the username/cookie name so that it is publicly accessible across all the pages? This username will obviously change with each new user and is not constant.
I have tried doing this using external JS file but in every new page the value is reset to default which I don't want.
The exact solution to my problem is like the work done by:
public static variable
in Java (not final). I want to achieve this in JS.
There is no such thing in Javascript unless you use a storage API (client side storage, or cookies, or something like that). The reason is that when you move from one page to another, it doesn't particularly matter to the browser. It wipes its slate and starts over, keeping explicitly stored data like cookies and such, and deleting everything else that is dynamically created. So the short of it is, if you want each page to know the name, you have to include the name in each page's code (manually or via script).
I don't quite know your application. In javascript you have function prototypes. In a function prototype you can declare 'static' members like so:
function C(params for constructor){
C.aStaticVariable = 5;
}
alert(C.aStaticVariable); //available everywhere
I have two HTML pages. After entering few inputs users will be redirected from first page to second page. Before redirecting the user to second HTML page(using window.location="new HTML URL"), I persist few of the user inputs in cookie using document.cookie DOM API.
When I am in the second HTML page, I could not retrieve the value from this cookie. I think since document object would have changed in the new HTML page, my cookie values become inaccessible.
Can someone tell me: how do I retrieve the value from a cookie persisted by one javascript in one HTML page in other HTML page i.e cookie written by HTML A's javascript in HTML B's javascript?
I don't have any server-side code, so I could not take advantage of server-side logic. Also I am not supposed to pass the values in URL. So I need a solution on plain javascript and HTML.
If some one has a better solution please let me know. Thanks
try to use localStorage instead of cookies,
// set your values in the first page
localStorage.setItem('itemKey', 'values');
// on the second page, retrieve them
var values = localStorage.getItem('itemKey');
you can use a jStorage plugin for cross browser behaviour.
also refer to this question for storing objects instead of strings
JAAulde is on point with his answer.
For what the OP is trying to do something like PHP would be great, in that case I wouldn't bother with cookies in order to just pass data between two pages, that's just silly. However, if true persistence was needed and the data requirements were simple cookies would be the way to go even while using a language such as PHP.
Those are rather draconian constraints, is this a class project? That said there aren't any other ways to do what you're attempting, save for an ugly and highly insecure hack of the DOM.
I have a temp.js file containing global variable 'num' which is intially set to 0. It has a function Increment() which increments the num by 1.
I have 2 html files Page1.html and Page2.html. Both are referring to temp.js file (). Both the pages are having a button on click of which I call a javascript and navigate to the other page. i.e, on button click from Page1.html I navigate to Page2.html and vice versa.
Before navigating from any page, I call Increment() function. What I see is num is always intialized to 0 and Increment function sets it to 1. What I want is num be set to 0 for the very first time and later increment by 1 anytime I call the function.
Anyway that I can acheive the above. Passing the value using querystring or maintaining masterpage of some kind is ruled out in my scenario.
You can write the cookies. That is the best you can do.
Your best bet would be to write the variable to a cookie. Note that this is one of the easiest spoofable methods that exist, so if the counter is in any way "important" you should refrain to do this.
In this last case your best bet would be to store the counter in a session variable. That would need 1) a server-side language (e.g. PHP, ASP, Perl) 2) an asynchronous call (AJAX) to a page on the server that updates the session.
You could also have a master page, which iframed Page1.html. When you browse to Page2.html in the iframe, the parent page's context will still exist. You can then reference your global counter from the two documents in the frame using parent.counter.
However, using an iframe would add one additional http request on page load, as well as incur additional resources consumed by the browser. It would also complicate your site. A cookie solution would add bytes to every request you issued to the server. Your choice.
I need to pass a URL and domain type from one HTML page to another and apparently the best way to do this is using java-script to create a cookie and pass the value to the next HTML page, but I do not know anything about cookies or even where to start. Where can I find a good guide to get me started with cookies and/or how to pass the value from one form to another? Is there a simpler way instead of using cookies? Thanks!
There is more than one way to do this:
- You can pass the values using GET or POST variables.
- Save the value in the session (depending on the language this can be more or less easier).
- Or save it to a cookie.
I think the easiest way to do it is to use POST variables (is as easy as with GET but you should avoid passing an URL in the url itself).