Link to home
Start Free TrialLog in
Avatar of webguyfred
webguyfred

asked on

Can Javascript detect a change in a variable?

Does anyone know a snippet of code where javascript can detect if the value of a variable has change when the page containing the javscript code is refreshed? and pop up an alert if change is detected?
Avatar of HonorGod
HonorGod
Flag of United States of America image

By variable, do you mean a document element, or a javascript variable?

You could set up a interval timer that, when started, copies the variable being watched somewhere, and checks if the value has changed.

Interesting question.

Do you need code?

What kind of values need to be watched?
Avatar of ftaco96
ftaco96

Where is the changing variable located? Server? Client? Database? Same webpage? Different webpage?
The items mentioned by ftaco96 are difficult, and possibly very expensive (in terms of effort, and resource consumption).  So, I'm hoping that you mean either a javascript variable on the client (browser) page, or a document element.

For the latter, many document elements have an "onchange" event handler that can be used.


<input type='text' onchange='checkValue(this.value)'>

Open in new window

webguyfred,
I only mention all of those because of the way the question is worded...
"when the webpage containing the javascript code is refreshed"
...and the AJAX tag you have on this question.

Every time you reload a web page, variables and elements on the page are reinitialized. A variable or element on the web page that has changed will lose its state, unless saved to some other place (session, db, cookie, etc).
Refreshing a page means - the page gets reloaded and the data you had modified in the variable also gets refreshed. Refreshing is not option to find the changes in variable. if you have a timer on the page then the data can be posted to the same page and then you can check the contents of variable at the page load and take the necessary (popup) action. Hope this helps. write back...
Avatar of webguyfred

ASKER

Thanks so far for the responses. The variable is defined by a perl script, which outputs an html page containing the javascript -- it's part of a shoutbox script and I'm trying to have the javascript notice when $high_message changes and would then reload the main chat frame.

It seems the consensus it to save the current $high_message somewhere (a cookie?) and then check that every couple seconds and if it changes then refresh the chat page.

The idea is to only refresh the chat page when the javascript/cookie notices the $high_message changing.
Yeah, you are definitely going to need a mechanism for the communication of the variable value from the Perl script environment to the Javascript environment.  A cookie is a reasonable way to go.

Do you know how to do cookies in JavaScript?
Do you know how to call a specific routine given some time interval (e.g., every few seconds)?
It all depends on the source of the change to the variable.

- If the source of the change to the $high_message var is on the server, then the client code (js) has to poll the server (perl script) to find out if there's a change. That's what AJAX is designed for.
- Cookies would work best if the source of the change were some user action that happened on a different webpage or a previous run of the current webpage.

It sounds like your perl script is aware of this variable, which means that you can write a tiny perl script page that prints out only that value and your javascript can make an AJAX call to that page to get it.
Yes I have the current $high_message embedded in the html that perl outputs containing the javascript/AJAX.

And also output the current $high_message to a text file, like high_message.txt

Then I guess AJAX could compare that $high_message outputted into the html with the current $high_message in high_message.txt every few seconds, and refresh the page when they differ?

Being new to AJAX, do you know of any code that comes close to that?
ASKER CERTIFIED SOLUTION
Avatar of ftaco96
ftaco96

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all responses. Each one helped. I think I'm going to try the AJAX examples to meld them into what I want to do. Wow, this forum really rocks.
Thanks for your link to the AJAX examples, and other advice. I'm going to try to make a script using the examples. Thanks again.
Just an update:  the examples in the link to W3 were a good intro to AJAX, but looks like I'll be getting a programmer I know to write his own code to do the exact the function i need, and to get it working with the PERL. Thanks again to this forum for helping me understand better the logic.