Asked  7 Months ago    Answers:  5   Viewed   25 times

Is there a way we can persist javascript variables across various pages? Suppose in Page A I am setting window.someVar = 5. Then I move to Page B, via clicking a hyperlink in A, and do something like alert(window.someVar) -- I should get a message box displaying 5. Is there a technique to persist someVar as such...?



You could use the window’s name to store the information. This is known as JavaScript session. But it only works as long as the same window/tab is used.

Tuesday, June 1, 2021
answered 7 Months ago

There are two obvious ways to maintain state in the browser without requiring that the server remember it between pages:

  1. Cookies

  2. localStorage

The latter is trivial to implement, but is only available in HTML5.

Note that neither is intended to be secure - a determined page hacker could set either to whatever value they wish.

Saturday, June 19, 2021
answered 6 Months ago

Can I use cookies for this ?

Yes, see this tutorial on using cookies in Javascript.

Are there other posibilities?

If you are not allowed to append anything the URL of your requests, I can't come up with any.

Where cookies are stored when Request is made ?

In the HTTP request header. The aforementioned tutorial will tell you how to read their values from Javascript. On the server side with ASP.Net, you can read cookie values using Request.Cookie["cookieName"] which returns an instance of HttpCookie.

Friday, August 27, 2021
answered 3 Months ago

I was able to fix this issue by making browser sleep for few seconds after every change in URL.

Below is the code snippet:

67 it('should create new job listing', function () {
68        //Login As Admin To Access Vacancies Feature
69        loginAsManager();
.        //load manager's dashboard list page
.        dashboardPage = new DashboardPage();
.        //load vacancies list page
.        var vacanciesUrl = browser.baseUrl + '#/vacancies';
.        browser.sleep(2000);
.        expect(browser.getCurrentUrl()).toEqual(vacanciesUrl);
.        vacanciesPage = new VacanciesPage();

I don't think this is a neat solution to this problem.

I will be happy to hear, if someone has a better solution to this.

Cheers Gaurav

Friday, October 15, 2021
Morrison Chang
answered 2 Months ago

Maybe a dependency error. I'll suggest you to use a better practice for such lib.

  1. Put the fullcalendar scripts into vendor/assets/javascripts

  2. Require this script manually in application.js before the tree and after jquery and any other lib it depends.

    /=require 'fullcalendar`


Check the loaded Javascript files in header, if application.js is the last, you have dependency error. The lib must be there before calling it.

Thursday, November 11, 2021
Yuriy Faktorovich
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :