how to find whether a value of variable is changed or not in javascript .
+1
A:
If you are a firefox user you can check using firebug. If you are using IE we can put alert statements and check the values of the variables.
Multiplexer
2010-06-16 06:28:12
+5
A:
Ehm?
var testVariable = 10;
var oldVar = testVariable;
...
if (oldVar != testVariable)
alert("testVariable has changed!");
And no, there is no magical "var.hasChanged()" nor "var.modifyDate()" in Javascript unless you code it yourself.
Max
2010-06-16 06:30:00
A:
By comparing it to a known state to see if it differs. If you're looking for something like variable.hasChanged, I'm pretty sure that doesn't exist.
deceze
2010-06-16 06:31:45
+2
A:
There is a way to watch variables for changes: Object::watch - some code below
/*
For global scope
*/
// won't work if you use the 'var' keyword
x = 10;
window.watch( "x", function( id, oldVal, newVal ){
alert( id+' changed from '+oldVal+' to '+newVal );
// you must return the new value or else the assignment will not work
// you can change the value of newVal if you like
return newVal;
});
x = 20; //alerts: x changed from 10 to 20
/*
For a local scope (better as always)
*/
var myObj = {}
//you can watch properties that don't exist yet
myObj.watch( 'p', function( id, oldVal, newVal ) {
alert( 'the property myObj::'+id+' changed from '+oldVal+' to '+newVal );
});
myObj.p = 'hello'; //alerts: the property myObj::p changed from undefined to hello
myObj.p = 'world'; //alerts: the property myObj::p changed from hello to world
// stop watching
myObj.unwatch('p');
meouw
2010-06-16 07:33:51
Not supported in IE and Safari.
Max
2010-06-16 08:09:22