Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7798557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:02:51+00:00 2026-06-02T00:02:51+00:00

I have a fairly simple task to replace specif texts on a page. Say,

  • 0

I have a fairly simple task to replace specif texts on a page. Say, there are 10 DIVs, each of which containing a specific information. I want to check this information against a database, and replace the text with a result from the database.

I place GM_xmlhttpRequest inside a loop that should check this information and replace it for each of the 10 DIVs. Unfortunately, this doesn’t work and the last DIV contains 10 very same information. In other words, GM_xmlhttpRequest is executed 10 times when i=10.

Below is a simplified code:

var i=1;
while (i<=10) {
  var id = 'div-' + i; //This sets the name of the DIV
  var ShowContent = document.getElementById(id);
  GoToURL = "http://domain.com/?=" + id; //This specifies a URL that changes on every loop
  GM_xmlhttpRequest({
    method: "GET",
    url: GoToURL,
    onload: function(response) {
      ShowContent.innerHTML = response; //This should replace the information for each DIV
      alert(i); //TEST: this shows always 11 (even not 10!). Why?
    }
  });
  i++;
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:02:53+00:00Added an answer on June 2, 2026 at 12:02 am

    Ajax is asyncronous, so when response will be delivered, your loop will be already ended (so i=11).

    But you can use closure for handling 'i' in response function:

     var i=1;
     while (i<=10) {
       (function(i){
         var id = 'div-' + i; //This sets the name of the DIV
         var ShowContent = document.getElementById(id);
         GoToURL = "http://domain.com/?=" + id; //This specifies a URL       
         GM_xmlhttpRequest({
             method: "GET",
             url: GoToURL,
             onload: function(response) {
                ShowContent.innerHTML = response; //This should replace the information for each DIV
                alert(i); 
             }
         });
       })(i);
      i++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly simple task in which I need to select rows with
I have a fairly simple set of functionality for which I have multiple implementations,
I want to automate a fairly simple task. For this I have written a
I have a fairly simple task: I need to read a PDF file and
I have a fairly simple, though expensive, task that I need to run in
I have a fairly simple ASP.NET 2.0 menu control using a sitemap file and
I have a fairly simple const struct in some C code that simply holds
I have a fairly simple addition to the HTTP standard. An ambitious goal I
I have a fairly simple sync problem. I have a table with about 10
I have a fairly simple block of code. Sub Run(Name) on error resume next

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.