Solved

Can Javascript detect a change in a variable?

Posted on 2009-05-12
13
1,196 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
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
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
Comment Utility
Where is the changing variable located? Server? Client? Database? Same webpage? Different webpage?
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
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
 
LVL 9

Expert Comment

by:ftaco96
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
This might be a good place to start...
http://www.w3schools.com/Ajax/ajax_examples.asp
0
 

Author Comment

by:webguyfred
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now