Setattribute('src','page.html') Is Not Working
I have the following JavaScript to rotate pages in a iframe tag every 5 seconds. function setPage() { if (i == pages.length) { i = 0; } alert(pages[i]); //verif
Solution 1:
I'd suggest you to use elmnt.src = pages[i]
instead.
If it still gives you error, then most probably you are trying to target element, that doesn't have src
property. Check that elemt.tagName
gives you IFRAME
.
Solution 2:
Have you tried just manually setting the src property of the iframe?
document.getElementById('dashboard').src = pages[i];
Solution 3:
As you have it now, each time setPage
gets called, the value i
is undefined
; if you want the value of i
to be held from call to call, you need to set it in a closure:
var setPage = (function () {
var i = 0;
returnfunction () {
if (i == pages.length) {
i = 0;
}
var elmnt = document.getElementById('dashboard');
elmnt.setAttribute('src', pages[i]);
i++;
}
}());
Also when setting the interval, the first argument should just be the name of the function, no quotes or parens:
setInterval(setPage, 5000);
There's a couple other tweaks you could make to it, but that should get it running.
Post a Comment for "Setattribute('src','page.html') Is Not Working"