PDA

View Full Version : Keep writing elements into an iframe even after its redirection to different URLs



Rain Lover
03-15-2014, 05:24 PM
I know how to write into an iframe:


<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Writer</title>
<style>
textarea,
iframe {
display: block;
width: 800px;
height: 200px;
}
</style>
</head>

<body>
<textarea id="ta" oninput="writeIt();"></textarea>
<iframe id="frm"></iframe>
<script>
function writeIt() {
var ta = document.getElementById('ta');
var frm = document.getElementById('frm');
var frmDoc = frm.contentDocument;
frmDoc.open();
frmDoc.write(ta.value);
frmDoc.close();
}
</script>
</body>

</html>

DEMO (http://jsfiddle.net/Mori/VY7uw/)

There's a problem, though. Enter the following into the textarea:

<a href="http://www.example.com/">Example.com</a>

Now click on the link, and then get back to the textarea to continue writing. It doesn't work anymore due to the cross-origin restriction. Is there any way in this case to continue writing?

jscheuer1
03-16-2014, 04:54 PM
No. You cannot write to a page on another server. Not like that. You would need FTP or other server access (any of which is usually password protected) to that other server in order to do that. An iframe altered via javascript doesn't have that capability even when no password is required. If the other server was running/had available on it server side code (that would do that) that you could access from the server your page is on, then you could do that. Or if the permissions were open for server side code available on your page's server, one could copy the other page from the other server, add to it, and save and/or display that copied and altered content on your server - subject as well to copyright rules and/or anti fraud regulations if applicable.

Rain Lover
03-17-2014, 03:36 AM
No. You cannot write to a page on another server. Not like that. You would need FTP or other server access (any of which is usually password protected) to that other server in order to do that. An iframe altered via javascript doesn't have that capability even when no password is required. If the other server was running/had available on it server side code (that would do that) that you could access from the server your page is on, then you could do that. Or if the permissions were open for server side code available on your page's server, one could copy the other page from the other server, add to it, and save and/or display that copied and altered content on your server - subject as well to copyright rules and/or anti fraud regulations if applicable.

Many thanks for the answer, John! However, I've found three similar applications that seem to function as I wish. I wonder how they work differently from my demo and what changes I need to make in my code:


https://thimble.webmaker.org/
http://codepen.io/pen/
http://cssdeck.com/labs


I tried the following code on them:


<a href="https://googledrive.com/host/0B1iqp0kGPjWsVms5S3JzaUZJYVk/page-template.html" target="_self">Page Template</a>

Rain Lover
03-17-2014, 03:42 PM
No. You cannot write to a page on another server. Not like that. You would need FTP or other server access (any of which is usually password protected) to that other server in order to do that. An iframe altered via javascript doesn't have that capability even when no password is required. If the other server was running/had available on it server side code (that would do that) that you could access from the server your page is on, then you could do that. Or if the permissions were open for server side code available on your page's server, one could copy the other page from the other server, add to it, and save and/or display that copied and altered content on your server - subject as well to copyright rules and/or anti fraud regulations if applicable.

Is it reliable to reclaim the iframe like this: demo (http://jsfiddle.net/Mori/VY7uw/4/).
Or this way: demo (http://jsfiddle.net/Mori/VY7uw/6/).