admin管理员组文章数量:1327849
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]); //verify the right url is there
var elmnt = document.getElementById('dashboard');
elmnt.setAttribute('src', pages[i]);
i++;
}
setInterval("setPage()", 5000);
The loop, interval, etc., is working. However, nothing changes for the src
attribute of my iframe
tag.
I tested with both IE8 and Chrome.
What am I doing wrong? How can I acplish that (no jQuery...)
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]); //verify the right url is there
var elmnt = document.getElementById('dashboard');
elmnt.setAttribute('src', pages[i]);
i++;
}
setInterval("setPage()", 5000);
The loop, interval, etc., is working. However, nothing changes for the src
attribute of my iframe
tag.
I tested with both IE8 and Chrome.
What am I doing wrong? How can I acplish that (no jQuery...)
Share Improve this question asked Mar 22, 2013 at 15:39 AmarundoAmarundo 2,39716 gold badges52 silver badges70 bronze badges 8-
Have you tried
elmnt.src = pages[i]
? – VisioN Commented Mar 22, 2013 at 15:40 -
Rather verify that
elmnt
is there. Do you get any errors in the console? – Bergi Commented Mar 22, 2013 at 15:41 - @VisioN - yes, I did: "Object doesn't support this property or method" – Amarundo Commented Mar 22, 2013 at 16:03
-
@Amarundo It means that
elmnt
is not the element you require. – VisioN Commented Mar 22, 2013 at 16:04 -
@Bergi - no errors - I even
alert(elmnt.id)
and I get "dashboard" – Amarundo Commented Mar 22, 2013 at 16:04
3 Answers
Reset to default 2I'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
.
Have you tried just manually setting the src property of the iframe?
document.getElementById('dashboard').src = pages[i];
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;
return function () {
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.
本文标签: javascriptsetAttribute(39src3939pagehtml39) is not workingStack Overflow
版权声明:本文标题:javascript - setAttribute('src','page.html') is not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742218819a2435061.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论