467,174 Members | 1,339 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,174 developers. It's quick & easy.

write to a window.open()

Hello there,
I am very new to Javascript and before I explain what I want I'll tell
you in a few words that I am trying to make a button on a WYSIWYG text
editor (RichArea) that previews on a template the content of the editor
and not in just an empty page.

I can't get around the folowing:

I have a page with a form and a text area. In the textarea I write html
code or any text.
Then I hit a button and I open a new window which is a template of a
page and I want to put the content of the textarea from the previous
page inside a content div.

This is the code of the source page:
/********************STARTOF CODE *********************/
<script type="text/JavaScript">
<!--
//dont need to restore borders in change_mode - works faster
function get_content(editor) {
if (!editor)
return '';
var text_area_content;
eval('var text_area = document.getElementById(editor);');
text_area_content = text_area.value;
return text_area_content;
}
function open_window() {
content = get_content('Contentbody');
pre_window = window.open('test_preview.php', 'PREVIEW');
}
//-->
</script>
</head>
<body>
<form action="<? $_SERVER['PHP_SELF'] ?>" name="add_content" method="post">
<textarea name="Contentbody" id="Contentbody" cols="80" rows="10"
wrap="virtual"></textarea>
<br />
<input type="button" name="submit" value="PREVIEW"
onclick="open_window()"/>
</form>
</body>
/********************END OF CODE *********************/
And this is the code for the preview page:
/********************START OF CODE *********************/
<table id="container" border="0" cellpadding="0" cellspacing="0">
<tr><td colspan="2"><div id="header">HEADER</div></td></tr>
<tr valign="top">
<td id="leftDIV">MENU</td>
<td id="content">
<script type="text/JavaScript">
<!--
var text_area = window.opener.document.add_content.Contentbody.val ue;
document.write(text_area);
//-->
</script>
</td>
</tr>
<tr><td colspan="2"><div id="footer">FOOTER</div></td></tr>
</table>
/********************END OF CODE *********************/

That script will do the job... but that's not an option for me because
the form name and textarea name will be Dynamic so
"window.opener.document. add_content.Contentbody. value;"
is not possible.

What I want to do is something like that in the first page:
content = get_content('Contentbody');
pre_window = window.open('test_preview.php', 'PREVIEW');
pre_window.document.write(content);
pre_window.document.close();

but document.write will delete everything when is called... so thats not
an option as well...

My next thought is:
content = get_content('Contentbody');
pre_window = window.open('test_preview.php', 'PREVIEW');
var div = pre_window.document.getElementById("content");
var text = pre_window.document.createTextNode("my text");
pre_window.document.div.appendChild(text);
pre_window.document.getElementById("content").inne rHTML=content;

But that doesn't work... If that works I think that will solve my
problemmm...
By the way the above code was inspired by a reply on a previous post
"document.write() in the middle of a document?"

Any IDeas ?
Thanks a lot
Angelos
Feb 17 '06 #1
  • viewed: 2598
Share:
1 Reply
Angelos wrote:
I have a page with a form and a text area. In the textarea I write html
code or any text.
Then I hit a button and I open a new window which is a template of a
page and I want to put the content of the textarea from the previous
page inside a content div.

This is the code of the source page:
/********************STARTOF CODE *********************/
<script type="text/JavaScript">
<!--
//dont need to restore borders in change_mode - works faster
function get_content(editor) {
if (!editor)
return '';
var text_area_content;
eval('var text_area = document.getElementById(editor);');
text_area_content = text_area.value;
return text_area_content;
}
function open_window() {
content = get_content('Contentbody');
pre_window = window.open('test_preview.php', 'PREVIEW');
}
//-->


First off, you don't need the <-- and //--> - take them out.

Now, as to the specific problem you're having:

You can address the new window as a variable - so where you have
pre_window = window.open('test_preview.php', 'PREVIEW');
you can now address that window as pre_window. For example:
pre_window.document.write(content);

There are, I think, better ways to handle writing the content (using
DOM), but that should at least get you a start.

Feb 17 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

By using this site, you agree to our Privacy Policy and Terms of Use.