Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a JavaScript Greasemonkey script that executes on an Amazon's results page. However, because the "Next Page" button dynamically updates the results, the script only executes the first time the page is loaded, and not anymore after the "Next Page" button is clicked.

For example, if I go to an Amazon page like this one and my script runs to do something with the results, then when I click "Next Page" at the bottom of the page, the script does not run anymore because the results are updated dynamically.

How can I make my script apply to the new set of results, whenever a new set is loaded when the user clicks the "Previous Page" or "Next Page" links?

share|improve this question

1 Answer

Amazon changes the results with AJAX and doesn't fire the hashchange event or even alter the title.

No matter; for this kind of thing, use the waitForKeyElements() utility.

For example, here is a simple script that alternates the results color:

// ==UserScript==
// @name     _Amazon: Stripe Search Results
// @include  http://www.amazon.com/s/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
waitForKeyElements ("#searchTemplate div.results div.result", stripeAlternateRows);

function stripeAlternateRows (jNode) {
    var rezRow  = jNode.attr ("id").replace (/result_/, "");
    rezRow      = parseInt (rezRow, 10)  ||  0;

    if (rezRow % 2) {
        //-- Color every odd row.
        jNode.css ("background", "#EEFFEE");
    }
}

Reference:

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.