admin管理员组文章数量:1429142
I've searched an answer to this question but can't get a way how to do it.
I want to access the content of a div that I have included in an object tag.
My include.htm file:
<div id="includedDiv">This is the included page</div>
What I have tried :
<html>
<head>
<title>Get element from object included page</title>
</head>
<body>
<p>Test: This is the main page</p>
<object id="obj" data="include.htm"></object>
<script>
//alert(document.getElementById("includedDiv").firstChild.nodeValue);
//alert((document.getElementById("obj")).document.getElementById("includedDiv").firstChild.nodeValue);
alert(document.getElementById("obj")["includedDiv"]);
</script>
</body>
</html>
None of the alert prints me the message "This is the included page", is there a way to get this content from the DOM?
EDIT: window[0].document.getElementById("includedDiv").firstChild.nodeValue; was the the good answer for access through the DOM, object tag just creates another window :) .asp
I've searched an answer to this question but can't get a way how to do it.
I want to access the content of a div that I have included in an object tag.
My include.htm file:
<div id="includedDiv">This is the included page</div>
What I have tried :
<html>
<head>
<title>Get element from object included page</title>
</head>
<body>
<p>Test: This is the main page</p>
<object id="obj" data="include.htm"></object>
<script>
//alert(document.getElementById("includedDiv").firstChild.nodeValue);
//alert((document.getElementById("obj")).document.getElementById("includedDiv").firstChild.nodeValue);
alert(document.getElementById("obj")["includedDiv"]);
</script>
</body>
</html>
None of the alert prints me the message "This is the included page", is there a way to get this content from the DOM?
EDIT: window[0].document.getElementById("includedDiv").firstChild.nodeValue; was the the good answer for access through the DOM, object tag just creates another window :) http://www.w3schools./jsref/prop_win_length.asp
Share Improve this question edited Aug 30, 2014 at 20:29 AstroCB 12.4k20 gold badges59 silver badges74 bronze badges asked May 19, 2012 at 12:19 baptxbaptx 3,9466 gold badges38 silver badges44 bronze badges 8- Possible clue, the content I want to access is on an external page, some people say object tag closes immediately, so there is no child to find stackoverflow./questions/1981191/… It seems possible to load content of an external HTML page with jQuery load method or using the native Javascript AJAX method XMLHttpRequest stackoverflow./questions/4728520/… – baptx Commented May 19, 2012 at 14:17
- I don't understand why we would plicate things, the div content is loaded, we can see the message on the page so it should be somewhere, accessible through the DOM! – baptx Commented May 19, 2012 at 14:23
- @baptx window[0].document.getElementById("includedDiv").firstChild.nodeValue; :) – baptx Commented May 25, 2012 at 12:33
-
Be advised that the ability to look into the
<object>
may be subject to same-origin policy. – Ulrich Schwarz Commented May 25, 2012 at 12:56 -
1
that's not the point. You may find it doesn't work for
... data="http://somewhere.else." ...
. – Ulrich Schwarz Commented May 25, 2012 at 13:38
3 Answers
Reset to default 0Got it! Just use
window.parent.mainPageMethod(document.getElementById("includedDiv").firstChild.nodeValue);
in the included page and you can get div content in a Javascript function from the main page!
Thanks for trying to help Jay, I should admit that when you talked about window.name property it put me on the right direction :)
document.getElementById('id').innerHTML
or document.getElementById('id').outerHTML
whichever you are looking for...
see how to append to a object with innerHTML
Even better answer: you CAN access object tag content through the DOM, it is just another window!
window[0].document.getElementById("includedDiv").firstChild.nodeValue;
That's it :D http://www.w3schools./jsref/prop_win_length.asp
本文标签: javascriptAccess external HTML page content of object tagsStack Overflow
版权声明:本文标题:javascript - Access external HTML page content of object tags - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745430375a2658297.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论