Brock's answer is good, but I'd like to offer another solution to the AJAX problem that is more modern and elegant.
Since his script, like most others, also uses setInterval() to check periodically (300ms), it can't respond instantly and there is always a delay. And other solutions uses onload events, which will often fire earlier than you want on dynamic pages.
You can use MutationObserver() to listen for DOM changes and respond to them as soon as the element is created
(new MutationObserver(check)).observe(document, {childList: true, subtree: true});
function check(changes, observer) {
if(document.querySelector('#mySelector')) {
observer.disconnect();
// code
}
}
Though since check() fires on every single DOM change, this may be slow if the DOM changes very often or your condition takes a long time to evaluate, so instead of observing document, try to limit the scope by observing a DOM subtree that's as small as possible.
This method is very general and can be applied to many situations. To respond multiple times, just don't disconnect the observer when triggered.
Another use case is if you're not looking for any specific element, but just waiting for the page to stop changing, you can combine this with a countdown that resets every time something changes on the page.
var observer = new MutationObserver(resetTimer);
var timer = setTimeout(action, 3000, observer); // wait for the page to stay still for 3 seconds
observer.observe(document, {childList: true, subtree: true});
// reset timer every time something changes
function resetTimer(changes, observer) {
clearTimeout(timer);
timer = setTimeout(action, 3000, observer);
}
function action(observer) {
observer.disconnect();
// code
}
This method is so versatile, you can listen for attribute and text changes as well. Just set attributes and characterData to true in the options
observer.observe(document, {childList: true, attributes: true, characterData: true, subtree: true});