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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745430375a4627362.html
评论列表(0条)