?
Solved

Can Javascript detect a change in a variable?

Posted on 2009-05-12
13
Medium Priority
?
1,206 Views
Last Modified: 2012-05-06
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?
0
Comment
Question by:webguyfred
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 24366756
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?
0
 
LVL 9

Expert Comment

by:ftaco96
ID: 24366887
Where is the changing variable located? Server? Client? Database? Same webpage? Different webpage?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24366966
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

0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 9

Expert Comment

by:ftaco96
ID: 24367233
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).
0
 

Expert Comment

by:gskaccount
ID: 24367331
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...
0
 

Author Comment

by:webguyfred
ID: 24368320
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.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24368562
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)?
0
 
LVL 9

Expert Comment

by:ftaco96
ID: 24368894
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.
0
 

Author Comment

by:webguyfred
ID: 24369442
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?
0
 
LVL 9

Accepted Solution

by:
ftaco96 earned 2000 total points
ID: 24369485
This might be a good place to start...
http://www.w3schools.com/Ajax/ajax_examples.asp
0
 

Author Comment

by:webguyfred
ID: 24374784
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.
0
 

Author Closing Comment

by:webguyfred
ID: 31580610
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.
0
 

Author Comment

by:webguyfred
ID: 24374909
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.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question