aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes scope issues? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "scope issues?" Watch "scope issues?" New topic
Author

scope issues?

Sam Doder
Ranch Hand

Joined: Feb 06, 2008
Posts: 204
Ok , need to mantain a javascript array over multiple pages.

I have tried creating an external javascript file with functions that store/retrieve the variable/array but when I try to get the value of the array I stored.
I get null if I am getting it from another page
This is in my external js file that is included in both my html pages


The problem is the page that creates the array and stores it everything works fine but when the other html tries to get the array It gets null... meaning the varible is not remaining for both html file.

How can I get a varible to remain across multiple pages in javascript???
Thanks
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
JavaScript does not maintain state between page refreshes/loads.

When you leave the page everything is reset.

If you need to maintain state, you need to look at a way to transfer data to the server or store it locally with cookies [or local storage in modern browsers]

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Until the browsers support HTML5 storage, you can't.

Imagine that the browser is an Etch-A-Sketch -- it gets wiped clean every time a new page is loaded.

You might use cookies for small snippets of data, or server-side session storage if you're using Servlets/JSP.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sam Doder
Ranch Hand

Joined: Feb 06, 2008
Posts: 204
Basically I need away to save a javascript array of values. Between html pages but not going back to the server using a jsp,asp,or php page.

I can use document.cookies but this is unstable since their is no garrenty that the user has cookies enabled or security not blocking cookies ,...etc

Is their any other javascript alternative from other then document.cookies that does the same.

Basically is their anyway that you can access a js files varibles from an external javascript function/file or any other thing that you could do to save varible info/state in javascript ?


If none of this is possible then what is the best/easiest way to translate a javascript array/variable to a jsp session varible or something so I can at least have the jsp serverside stuff keep track of the state of it. (translating javascript varibles into jsp session attributes on the fly seems difficult I know no way to do it easyly in general.

Thanks

Curious in HTML5 if their is away to maintain javascript varible state over multiple html pages?



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Sam Doder wrote:Basically is their anyway that you can access a js files varibles from an external javascript function/file or any other thing that you could do to save varible info/state in javascript ?

Were you not just told "no"?

If none of this is possible then what is the best/easiest way to translate a javascript array/variable to a jsp session varible or something so I can at least have the jsp serverside stuff keep track of the state of it.

Store in session as JSON?
Sam Doder
Ranch Hand

Joined: Feb 06, 2008
Posts: 204
Store in session as JSON?


are you saying just to store the javascript array in a session varible in my jsp page.
If so how can I transfer a javascript varible into this session.setattribute(...) ???

I don't know how to translate javascript into jsp java ?

Is JSON something different then javascript???

thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

JSON can be represented by a Java string:If that string is in session, it can be used in a JSP as a JavaScript value:Building the JSON should be done with an existing library.
Sam Doder
Ranch Hand

Joined: Feb 06, 2008
Posts: 204
well that is not really what I can use.

My problem is I have a popup menu that contains an iframe with check boxes on it. On the iframe page I have javascript to keep track of which buttons are check and which are unchecked. The apply button js function on the div tag on the main html page must beable to have access to the javascript array on the iframe page.... This is where my issue is?

so the issue is getting the array on the iframe page to be accessible on the main page javascript functions???



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

It's a lot easier for the page in the iframe to access the parent page via top.
Sam Doder
Ranch Hand

Joined: Feb 06, 2008
Posts: 204
well I need away so when the person clicks the apply button on the main page to have the js array on the iframe page accessible to the button on click javascript function.

The apply button is on the main page but the checkboxes / js array is on the iframe page.

How can the onclick button function access the js array varible on the iframe page?

Thanks

I don't know what your getting at with top ,... I have used document.getElementById("frame_menu").contentDocument; to get the iframe page but I don't know how to get a js varible from the button onclick event on the main page.... it returns null or not an object.... (meaning the varible does not exist or is not in scope.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Access from parent to child page is tricky. You might be better off having the element register itself with the parent page (by calling a function in the parent perhaps) upon load.

Or better yet, back off from the iframe if you can. Could you perhaps use a scrolling div rather than an iframe?
Sam Doder
Ranch Hand

Joined: Feb 06, 2008
Posts: 204
well, I kind of need to keep the iframe alot of code would have to be rewritten.
if possible...

ya but I see what your saying had I know that iframes / js scope was going to be an issue I would have started with the scrollable div tag.

damn
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
How is an iframe and a scrollable div tag any different between page refreshes? Is just the iframe changing and the parent page staying the same? If that is the case, you need to save your state in the parent.

Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: scope issues?