are you not in control of the content of your pages? As far as I
know, the only way to do something like this would be to read the
entire page and search to make sure the div exists before continuing.
Unfortunately you can't just execute the rjs call and expect it to
throw an exception on error (the exception is thrown outside the
context of rails and handled directly by the prototype library)
You can use the following library to allow you to parse your html files:
but of course it'll be faster when operating on local files than
through using open-uri. But this is an expensive operation which you
really want to avoid if possible.
Parse the entire page with hpricot? That's rather silly. You can use
basic javascript or the prototype library to check for the existance
of an element with an id, and you can use use the concatenation
operator to add plain javascript to the output: