By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,087 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,087 IT Pros & Developers. It's quick & easy.

Need a javascript guru (I need help!)

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.