Hi I need a jQuery function to add a text to src of all images in the page.
for example after one minute of load of my page I want to add "mobileserver"
to src of all images like this.
<img src="mobileserver/images/sampleimage.jpg" />
Hi I need a jQuery function to add a text to src of all images in the page.
for example after one minute of load of my page I want to add "mobileserver"
to src of all images like this.
<img src="mobileserver/images/sampleimage.jpg" />
You can use setTimeout()
to set delay and attr()
with callback function for updating the src
attribute,
$(document).ready(function () {
setTimeout(function () {
$('img').attr('src',function (i,v) {
return 'mobileserver/' + v;
});
}, 60000);
});
JQUERY ANSWER
As Wolff stated there no point in using setInterval
instead of setTimeout
$(document).ready(function () {
setTimeout(function () {
$('img').each(function () {
var nSrc= 'mobileserver/' + $(this).attr('src');
$(this).attr('src', nSrc);
});
}, 60000);
});
EDIT
Since my answer wasn't fully correct, I'm trying to deserve these points, so here it is a pure javascript solution:
(function () {
setTimeout(function () {
var imgTag = document.getElementsByTagName("img"),
count = imgTag.length;
for (var i = 0; i < count; i++) {
imgTag[i].setAttribute("src", 'mobileserver/' + imgTag[i].getAttribute('src'));
}
}, 60000);
})();
The only problem could be the browser compatibility, check this answer for other method to check if the document is ready:https://stackoverflow.com/a/9899701/1139052