Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Post Timeline

Post Reopened by Svisstack, Abdul Jabbar, Arun P Johny html Users with the html badge or a synonym can single-handedly close questions as duplicates and reopen them as needed.
added 390 characters in body; edited title
Source Link
Abdul Jabbar
  • 6k
  • 9
  • 57
  • 84

This is not a duplicate question. I have been working on loading images after the complete page load. I want to pass variable i value to the onload function of img but somehow that's not working.

var imgs = document.getElementsByTagName('img');
var totalImages = imgs.length;
for(var i=0; i<totalImages; i++)
{
 if(imgs[i].hasAttribute('data-src'))
 {
 var img = new Image();
 img.onload = function()
 {
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
 };
 img.src = imgs[i].getAttribute('data-src');
 }
}

I tried following onload function

img.onload = (function(x)
{
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
})(i);

but in this way, I saw that function is executed immediately even the image is not completely loaded may be because this is somehow working as anonymous function.

I also tried by setting attribute to my img object and getting its value inside function like this

img.setAttribute('idi',i);
img.onload = function()
{
 var x = img.getAttribute('idi');
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
};

but that's not working as well. How can I pass variable i value to my onload function?

I have been working on loading images after the complete page load. I want to pass variable i value to the onload function of img but somehow that's not working.

var imgs = document.getElementsByTagName('img');
var totalImages = imgs.length;
for(var i=0; i<totalImages; i++)
{
 if(imgs[i].hasAttribute('data-src'))
 {
 var img = new Image();
 img.onload = function()
 {
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
 };
 img.src = imgs[i].getAttribute('data-src');
 }
}

I tried following onload function

img.onload = (function(x)
{
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
})(i);

but in this way, I saw that function is executed immediately even the image is not completely loaded may be because this is somehow working as anonymous function.

How can I pass variable i value to my onload function?

This is not a duplicate question. I have been working on loading images after the complete page load. I want to pass variable i value to the onload function of img but somehow that's not working.

var imgs = document.getElementsByTagName('img');
var totalImages = imgs.length;
for(var i=0; i<totalImages; i++)
{
 if(imgs[i].hasAttribute('data-src'))
 {
 var img = new Image();
 img.onload = function()
 {
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
 };
 img.src = imgs[i].getAttribute('data-src');
 }
}

I tried following onload function

img.onload = (function(x)
{
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
})(i);

but in this way, I saw that function is executed immediately even the image is not completely loaded may be because this is somehow working as anonymous function.

I also tried by setting attribute to my img object and getting its value inside function like this

img.setAttribute('idi',i);
img.onload = function()
{
 var x = img.getAttribute('idi');
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
};

but that's not working as well. How can I pass variable i value to my onload function?

preeeetty sure it is a duplicate
Source Link
Rhumborl
  • 16.7k
  • 4
  • 42
  • 47

Pass variable to function in Javascript (not duplicate)

This is not a duplicate question. I have been working on loading images after the complete page load. I want to pass variable i value to the onload function of img but somehow that's not working.

var imgs = document.getElementsByTagName('img');
var totalImages = imgs.length;
for(var i=0; i<totalImages; i++)
{
 if(imgs[i].hasAttribute('data-src'))
 {
 var img = new Image();
 img.onload = function()
 {
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
 };
 img.src = imgs[i].getAttribute('data-src');
 }
}

I tried following onload function

img.onload = (function(x)
{
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
})(i);

but in this way, I saw that function is executed immediately even the image is not completely loaded may be because this is somehow working as anonymous function.

How can I pass variable i value to my onload function?

Pass variable to function in Javascript (not duplicate)

This is not a duplicate question. I have been working on loading images after the complete page load. I want to pass variable i value to the onload function of img but somehow that's not working.

var imgs = document.getElementsByTagName('img');
var totalImages = imgs.length;
for(var i=0; i<totalImages; i++)
{
 if(imgs[i].hasAttribute('data-src'))
 {
 var img = new Image();
 img.onload = function()
 {
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
 };
 img.src = imgs[i].getAttribute('data-src');
 }
}

I tried following onload function

img.onload = (function(x)
{
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
})(i);

but in this way, I saw that function is executed immediately even the image is not completely loaded may be because this is somehow working as anonymous function.

How can I pass variable i value to my onload function?

Pass variable to function in Javascript

I have been working on loading images after the complete page load. I want to pass variable i value to the onload function of img but somehow that's not working.

var imgs = document.getElementsByTagName('img');
var totalImages = imgs.length;
for(var i=0; i<totalImages; i++)
{
 if(imgs[i].hasAttribute('data-src'))
 {
 var img = new Image();
 img.onload = function()
 {
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
 };
 img.src = imgs[i].getAttribute('data-src');
 }
}

I tried following onload function

img.onload = (function(x)
{
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
})(i);

but in this way, I saw that function is executed immediately even the image is not completely loaded may be because this is somehow working as anonymous function.

How can I pass variable i value to my onload function?

Post Closed as "Duplicate" by Arun P Johny html Users with the html badge or a synonym can single-handedly close questions as duplicates and reopen them as needed.
Source Link
Abdul Jabbar
  • 6k
  • 9
  • 57
  • 84

Pass variable to function in Javascript (not duplicate)

This is not a duplicate question. I have been working on loading images after the complete page load. I want to pass variable i value to the onload function of img but somehow that's not working.

var imgs = document.getElementsByTagName('img');
var totalImages = imgs.length;
for(var i=0; i<totalImages; i++)
{
 if(imgs[i].hasAttribute('data-src'))
 {
 var img = new Image();
 img.onload = function()
 {
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
 };
 img.src = imgs[i].getAttribute('data-src');
 }
}

I tried following onload function

img.onload = (function(x)
{
 imgs[x].src = imgs[x].getAttribute('data-src');
 imgs[x].setAttribute('srcLoaded','true');
 //where x is i value from for loop
})(i);

but in this way, I saw that function is executed immediately even the image is not completely loaded may be because this is somehow working as anonymous function.

How can I pass variable i value to my onload function?

default

AltStyle によって変換されたページ (->オリジナル) /