Need a javascript guru (I need help!)

J

Jarrod Hyder

Ok, I wrote my own weblog and I'm working on the web interface for
adding/editing my posts. I decided to add a little preview button...when
the button is clicked it is suppose to open a pop-up window. I wrote the
whole preview function in javascript so that I don't have to reload the
page to see a preview.

Here is the code I wrote that doesn't work at all:

Code:

function generate_preview() {
var newWin =
window.open('/weblog/pop_up.html','Preview','width=650,height=400');

var previewText = document.forms[0].logtext.value; var previewTitle =
document.forms[0].title.value; var previewIcon = document.preview.src;

var insertPoint_Text = newWin.document.getElementById('pre'); var
content_Text = newWin.document.createTextNode(previewText);
insertPoint_Text.appendChild(content_Text);

var insertPoint_Title = newWin.document.getElementById('title'); var
content_Title = newWin.document.createTextNode(previewTitle);
insertPoint_Title.appendChild(content_Title);

var insertPoint_Icon = newWin.document.getElementById('icon');
insertPoint_Icon.src = previewIcon;
}



And here is the code that works flawlessly to generate the preview:

Code:

function generate_preview() {
var newWin =
window.open('/weblog/pop_up.html','Preview','width=650,height=400');
newWin2=window.open();
newWin2.close();

var previewText = document.forms[0].logtext.value; var previewTitle =
document.forms[0].title.value; var previewIcon = document.preview.src;

var insertPoint_Text = newWin.document.getElementById('pre'); var
content_Text = newWin.document.createTextNode(previewText);
insertPoint_Text.appendChild(content_Text);

var insertPoint_Title = newWin.document.getElementById('title'); var
content_Title = newWin.document.createTextNode(previewTitle);
insertPoint_Title.appendChild(content_Title);

var insertPoint_Icon = newWin.document.getElementById('icon');
insertPoint_Icon.src = previewIcon;
}



It absolutely will not work unless I add these two lines:

Code:

newWin2=window.open();
newWin2.close();


It's really weird and I don't know how to fix the problem without opening
another window other than the one that I'm generating the preview in. I'm
not a programmer and I'm not that great with javascript, so if a
javascript guru could point me in the right direction I would really
appreciate it.
 
L

Lee

Jarrod Hyder said:
It absolutely will not work unless I add these two lines:

Code:

newWin2=window.open();
newWin2.close();


It's really weird and I don't know how to fix the problem without opening
another window other than the one that I'm generating the preview in. I'm
not a programmer and I'm not that great with javascript, so if a
javascript guru could point me in the right direction I would really
appreciate it.

window.open() returns before the window actually exists.

Those lines are creating enough of a delay to allow your newWin to
open and render content and be ready for you to start manipulating
its document object.

Since you're opening an HTML page in that window, one option would
be to put script in that page that loads the content from the
opener window, so that it pulls new content when it is ready,
instead of having the opener window start pushing it too soon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top