obj.watch() vs obj.onpropertychange()

Page Title:

I was trying to use javascript to do some trimming of content coming in on an RSS feed in SharePoint.  I created a function to do this which worked fine but I found that running it via window.onload was running before the RSS feed could load.   I needed a way to watch the div it loads in for a change of content.   Using the javascript watch function seemed like a reasonable solution but after much playing with it I realized IE doesn't appear to support it. 

Instead IE has it's own event, obj.onpropertychangeIn fact, in my case, this event worked better for me than obj.watch.   In the case of obj.watch, you specify the property to watch and the function to fire off when there's a change.  So you can do something like myDiv.watch('innerHTML', updateMyContent) to update some content when the content of the watched div changes.  I'm sure this is being done already on a lot of ajax-y sites, but it was new to me. 

The only problem in my case with obj.watch is that it expects a value back from the function and uses that value as the new property value.  In my function, I was trying to make changes to the content that had been changed which was then being overwritten when the function completed through the normal flow of obj.watch. 

For me, obj.onpropertychange worked much better.  It fires my function on a change, allows me to change the content and doesn't do any overwriting after I'm done.  Unfortunately, this only works in IE.  Fortunately in my case this is for an intranet where that's all that is used.  So just be aware of differences in functionality and browser support.

Relevant Tags: AJAX | JavaScript