Why can't I obtain the external nodes in an iframe? - javascript

I have this piece of code. I can see the iframe content but it seems that edp0 is always undefined. Why?
<!DOCTYPE html><html>
<body>
<iframe src="DOM-copyB.html"></iframe>
<script>
ed = document.getElementsByTagName('iframe')[0].contentDocument;
edp0 = ed.getElementsByTagName('p')[0];
edp1 = ed.getElementsByTagName('p')[1];
alert(edp0);
</script>
</body></html>
Here is DOM-copyB.html:
<!DOCTYPE html><html><head></head>
<body>
<p>A<b>B</b>C</p>
<p>1<b>2</b>3</p>
</body></html>

Your JavaScript is probably running before the iframe's content is loaded. Try running your code in window.onload and see if that helps:
window.onload = function() {
ed = document.getElementsByTagName('iframe')[0].contentDocument;
edp0 = ed.getElementsByTagName('p')[0];
edp1 = ed.getElementsByTagName('p')[1];
alert(edp0);
};
If you're not running the code from a server, though, I suspect after you get the code to successfully reference the iframe content, you'll get an error like this in the Chrome console due to Cross-Origin security:
Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.
If you run them from a server, that error should go away. See this question for more details on that:
Using iframe with local files in Chrome

I'm not sure about javascript. But jusing jQuery would be much easier:
<iframe src="DOM-copyB.html" id="myFrame"></iframe>
<script>
$("#myFrame").contents().find("p");
</script>

Related

Download extern webpage with JavaScript [duplicate]

This question already has answers here:
How to use JavaScript to access cross domain iFrame content?
(6 answers)
Closed 6 years ago.
I like to download the html code via JavaScript. This HTML-Code is an extern webpage which will be displayed in iframe. Is it possible to get HTML-Code how it is displayed in this iframe.
So here is my try:
<body>
<iframe id="iframe_htmlcode1" name="iframe_htmlcode" src="http://www.w3schools.com"></iframe>
<textarea id="TextArea1" name="S1"></textarea>
<script>
function getAndSetContent() {
var x = document.getElementById("iframe_htmlcode1");
var y = (x.contentWindow || x.contentDocument);
document.getElementById("TextArea1").value = y.documentElement.innerHTML;
}
</script>
<input id="Button1" type="button" value="button" onclick="getAndSetContent()" />
But y.documentElement.innerHTML returns nothing. I've tried it also with a local html document which will be used in the iframe, which works fine.
Any suggestions?
THANKS
I assume that your code is working with the local file because the protocols are matching, anyway the debug says it all (you will need some basic server and then try again).
Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "http://www.w3schools.com". The frame requesting access has a protocol of "file", the frame being accessed has a protocol of "http". Protocols must match.
Once you get your code on the server you may need to use something like the below to get around the cors issues
https://github.com/padolsey-archive/jquery.fn/tree/master/cross-domain-ajax

Refreshing an iframe which are in different domains

I have a page called main.jsp which is in domain domain1 and it has a iframe which loads contents from domain2. Basically main.jsp is a common contents and in iframe we load contents from other web applications deployed on different servers.
My problem is I want to refresh the content inside iframe automatically (say 5 seconds). I tried this code first:
<meta http-equiv="refresh" content="5;url=<s:url includeParams="all" />" />
Err: Blocked a frame with origin "http://localhost:8080" from accessing a cross-origin frame
I tried this code:
<script type="text/javascript">
window.setTimeout(function(){ window.location.reload() }, 15000);
</script>
Which also gives me same error. Can anyone guide me how to achieve this?
Note:
I have added this code to get rid of cross-domain issue:
<script type="text/javascript">
document.domain = window.location.hostname.replace('www.', '');
</script>
Try adding it dynamicaly
function addRefresh(){
var meta = document.createElement('meta');
meta.setAttribute("http-equiv", "refresh");
meta.setAttribute("content", "5");
document.getElementsByTagName('head')[0].appendChild(meta);
}
if(location.origin === 'domain1.com'){
addRefresh();
}

java script and same origin policy confusion

I am trying to understand the same origin policy of browsers. Theoretically things seem ok. So i am now trying to practically understand it using a small demo.
I have 2 domains hosted on wamp, viz domain1.com and domain2.com
domain1.com consists of index.php, innocent.php and 2 js files in a javascript folder, namely dom1_javascript.js and dom1_normal.js
here are the details of the above files: -
index.php
<?php
$value = "domain 1 cookie";
// send a simple cookie
setcookie("Dom1Cookie",$value);
?>
<html>
<script type="text/javascript" src="../javascript/dom1_javascript.js">
</script>
<body>
this is from doamin 1
</body>
</html>
innocent.php
<?php
$userSecret=$_GET['userCreds'];
if($userSecret)
{
echo "the user's secret is "+$userSecret;
}
else
{
echo "sorry user secret not found";
}
?>
dom1_javascript.js
alert(document.cookie);
dom1_normal.js
alert("alert domain 1");
alert(document.cookie);
//referring the div
var bdy=null;
// creating the form
var secretForm=document.createElement("form");
secretForm.id="goodForm";
secretForm.method="get";
var myQryStr="http://domain1.com/innocent.php?userCreds=abcd";
alert(myQryStr);
secretForm.action=myQryStr;
//creating the text box in the form
var hiddenBox=document.createElement("input");
hiddenBox.type="text";
hiddenBox.name="secBox";
hiddenBox.value="abhinav";
//appending the box to the form
secretForm.appendChild(hiddenBox);
//appending the form to the div
bdy=document.getElementById("mydiv");
alert(bdy);
bdy.appendChild(secretForm);
//submitting the form
document.getElementById("goodForm").submit();
domain2.com consists of 2 versions of index.php, viz, index.php and index1.php
here are the details of the above php file: -
index.php
<?php
$value = "domain 2 cookie";
// send a simple cookie
setcookie("Dom2Cookie",$value);
?>
<html>
<head>
<script type="text/javascript" src="http://domain1.com/javascript/dom1_javascript.js">
</script>
</head>
<body>
<div id="mydiv">
<img src="http://domain1.com/images/dom1.bmp"/>
this is from doamin 2
</div>
</body>
</html>
index1.php
<?php
$value = "domain 2 cookie";
// send a simple cookie
setcookie("Dom2Cookie",$value);
?>
<html>
<head>
<script type="text/javascript" src="http://domain1.com/javascript/dom1_normal.js">
</script>
</head>
<body>
<div id="mydiv">
<img src="http://domain1.com/images/dom1.bmp"/>
this is from doamin 2
</div>
</body>
</html>
I am using firefox as the browser to test these scritps.
First I goto domain1.com in the browser. This sets the domain1 cookie. Then I goto domain2.com/index.php
As expected, the script on domain2/index.php sets the domain2 cookie. Then the javascript from domain1 gets loaded which says
alert(document.cookie)
The execution of this script alerts the domain2 cookie value.
Assumption1: -
So my understanding here is that due to the same origin policy of the browser, even though the script was called from domain1, it did not alert the domain1 cookie, but instead alerted the domain2 cookie.
Please let me know if I am correct in the above assumption ?
Now I clear the browser cache and remove all the cookies from the browser. Run domain1.com again, which again sets domain1 cookie. And then, this time I goto domain2.com/index1.php which sets the cookie for domain2 and then accesses the script present in
domain1.com/javascript/dom1_normal.js
Now if my assumption1 was correct,( i.e. a javascript from domain1.com when imported in domain2.com will get executed with reference to domain2 only, and not its incoming domain, as per the same origin policy) then in this case also it should be the same with dom1_normal.js. So the javascript in dom1_normal.js should have access to all the HTML elements in domain2/index1.php It does not really so happen as confirmed by
bdy=document.getElementById("mydiv");
alert(bdy);
in domain1.com/javascript/dom1_normal.js which alerts null
please let me know where am i going wrong. And i have gone through more than a dozen discussions (on stack overflow and elsewhere including MDN, wiki, google etc.) and articles about same origin policy, but none of it has made the idea clear to me.
The Same-Origin policy doesn't have much to do with loading JavaScript. Regardless of where a script comes from, its actions take place under the aegis domain of the main page. Thus, if your main page comes from "domain1", then all scripts execute in the context of "domain1", whether they came from that domain or any other domain.
Note that it's not possible to access the source code of a script that loads from some other domain.
The reason your "dom1_normal" script reports "null" for that element reference is probably because you're importing the script before the <body>. The DOM is built incrementally, but scripts run synchronously when they're loaded, so if you call getElementById() for some element that's after the <script> tag, it won't be there.

PreloadJS in CreateJS is not working

I am trying to learn preloading of images using PreloadJS.
This is what I have:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script src="easeljs-0.6.0.min.js"></script>
<script src="preloadjs-0.3.0.min.js"></script>
<script src="soundjs-0.4.0.min.js"></script>
<script>
var stage, output, queue;
function handleComplete() {
var backgroundImage=new createjs.Bitmap("myImg");
stage.addChild(backgroundImage);
stage.update();
}
function init() {
stage = new createjs.Stage("demoCanvas");
queue = new createjs.LoadQueue();
queue.installPlugin(createjs.Sound);
queue.addEventListener("complete", handleComplete);
queue.loadFile({id:"myImg", src:"Background.png"});
}
</script>
</head>
<body onLoad="init();">
<canvas id="demoCanvas" width="1300" height="600">
Your browser does not support canvas
</canvas>
</body>
</html>
When I load the page, I get this error:
Access is denied: preloadjs-0.3.0.min.js, line 50 character 333
I traced the Call Stack and found out the error is generated from the following line:
queue.loadFile({id:"myImg", src:"Background.png"});
Can you tell me where have I gone wrong?
The path type (absolute/relative) shouldn't matter, however where you are loading it from might.
By default, PreloadJS will try and load content using XHR (XMLHttpRequests), which work only when loading on a server (file:// won't work, you should use localhost instead), and from the same domain. You can try passing false as an argument to LoadQueue to have it load the image using tags, which gets around most of those errors.
queue = new createjs.LoadQueue(false);
The errors you usually get from XHR loads are cross-origin errors though, not "Access is denied". If you are loading from your filesystem, make sure the file isn't read only, or have some sort of protection.

Get raw content of Iframe

I want to use Jquery or javascript to get the raw content (mean everycharacter) of an Iframe. It sounds simple but I'm still struggling with finding the right way for it.
For now it is only a XML content in the Iframe though.
Here the code:
$(function() {
var xmlContent = $("#CFrame").contents().find("*").text();
// The magic
$('#SResult').xslt({xml: xmlContent, xslUrl: 'stylesheet/designSS.xsl'});
});
The html page
<form id="searchForm" method="GET" target="ContentFrame" action="http://125.235.8.210:380/search" onSubmit="processContent()">
.....
</form>
</div>
<div id="SResult">
</div>
<iframe id="CFrame" name="ContentFrame" frameborder="1" height="2000px" width="1000px" scrolling="no" src="stylesheet/test.xml"></iframe>
</body>
Thanks,
Disclaimer: I'll answer your question regardless of whether it is actually an elegant solution to your problem. Joseph seems to take that as the question. I would say he is probably right to do so.
It won't work trying to get the frame using mimetype text/xml. The browser will proceed and 'translate' the XML into HTML. That's why it doesn't sound so simple. This way it is actually impossible.
I present you with a simple work-around for this problem.
<html>
<head>
<script>
function getXmlContents() {
/*
Note: Because of security reasons, the contents of a document can be accessed from another document only if the two documents are located in the same domain.
http://www.w3schools.com/jsref/prop_frame_contentdocument.asp
*/
var iframeDocument = document.getElementById('greetingFrame').contentDocument;
if (iframeDocument == null)
return undefined;
var xmlContainer = iframeDocument.getElementById('xmlContainer');
if (xmlContainer == null)
return undefined;
return xmlContainer.innerText == null ? xmlContainer.textContent : xmlContainer.innerText;
};
</script>
</head>
<body>
<iframe id="greetingFrame" src="helloworld.html" onload="alert(getXmlContents())">
</iframe>
</body>
</html>
The contents of the XML are wrapped inside an HTML (helloworld.html):
<html>
<body>
<script id="xmlContainer" type="text/xml">
<?xml version="1.0" encoding="UTF-8"?>
<title>
Hello world
</title>
</script>
</body>
</html>
I've successfully tested this in Chrome, Firefox and IE.
Of course you would have to wrap your XML documents inside a HTML script tag as indicated above. The XML can also be wrapped in a different tag, if you'd like it rendered for example, but you'd have to encode the XML using html encoding. This needs to be done on the server-side. A very simple (php/ruby/python/etc) script would suffice.
If your XML resides on your domain, you are better off with AJAX, especially using the jQuery library, which parses it for you and make it ready for immediate manipulations.
If it does not live on your domain, then you can't access it via AJAX unless the remote server and your client's browser both support CORS.
You have options though:
If the remote server's API supports JSONP, use it instead of XML. Then you can use jQuery to retrieve JSONP data or roll your own script loader.
Or use your server to proxy the XML for you. Servers are not restricted to the Same Origin Policy. Create an API on your server that relays your form data to the remote server and retrieve the remote page - all as if your server was the browser. Then forward the results back to you.

Categories