473,803 Members | 3,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

refering layer in new window

Hello NG,

I am using javascript to open a new window like this:

printpreview=wi ndow.open('prin tview.php','pri ntprev','width= 600,height=600' );

The new window has a layer called preview:

<div id="preview"></div>

I am now trying to write to that layer using innerHTML = "blah bla";

How do I refer to that layer?

Thanks,
Christian
Jul 23 '05 #1
6 1923
"news.versatel. de" wrote:
Hello NG,

I am using javascript to open a new window like this:

printpreview=wi ndow.open('prin tview.php','pri ntprev','width= 600,height=600' );

The new window has a layer called preview:

<div id="preview"></div>

I am now trying to write to that layer using innerHTML = "blah bla";

How do I refer to that layer?

Thanks,
Christian


It's a bad idea to do:

var newWindowRefere nce = window.open(... );
newWindowRefere nce.document.wr ite(...); // or
newWindowRefere nce.document.ge tElementById(.. .);

There is no guarantee that the new window is open, or has a valid document by the
time the code referring to it execute. So, for starters, printview.php should
have <body onload="if (opener && opener.callBack ) opener.callBack ();"> as a
minimum. Then, in the page that opened the new window, you can have:

window.newWindo wReference = window.open(... );
function callBack() {
if (document.getEl ementById) {
var theDiv =
window.newWindo wReference.docu ment.getElement ById('preview') ;
if (typeof theDiv.innerHTM L == 'string') {
theDiv.innerHTM L = 'some text';
}
}
}

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq

Jul 23 '05 #2
DU
Grant Wagner wrote:
"news.versatel. de" wrote:

Hello NG,

I am using javascript to open a new window like this:

printpreview= window.open('pr intview.php','p rintprev','widt h=600,height=60 0');

The new window has a layer called preview:

<div id="preview"></div>

I am now trying to write to that layer using innerHTML = "blah bla";

How do I refer to that layer?

Thanks,
Christian

It's a bad idea to do:

var newWindowRefere nce = window.open(... );
newWindowRefere nce.document.wr ite(...); // or


Don't you have to open the document and close it? Like
newWindowRefere nce.document.op en();
newWindowRefere nce.document.wr ite(...);
newWindowRefere nce.document.cl ose();
" Closes a document stream opened by open() and forces rendering."
http://www.w3c.org/TR/2003/REC-DOM-L...ml#ID-98948567
There is a problem when not closing the document in Mozilla-based
browsers and possibly other browsers as well.
newWindowRefere nce.document.ge tElementById(.. .);

There is no guarantee that the new window is open, or has a valid document by the
time the code referring to it execute. So, for starters, printview.php should
have <body onload="if (opener && opener.callBack ) opener.callBack ();"> as a
minimum. Then, in the page that opened the new window, you can have:

window.newWindo wReference = window.open(... );
newWindowRefere nce is already a type window (return value of the
window.open() call); so it is not needed to prefix it with window.
function callBack() {
if (document.getEl ementById) {
var theDiv =
window.newWindo wReference.docu ment.getElement ById('preview') ;
if (typeof theDiv.innerHTM L == 'string') {
The problem I see here (I might be wrong) is that *as written*, the
innerHTML value of that div id="preview" will be null: there is no
string when writing
<div id="preview"></div>.
Generally speaking, the structure
<div id="preview">&n bsp;</div>
is more manageable from a script perspective.
theDiv.innerHTM L = 'some text';


Also possible is branching DOM 3 textContent and innerText and/or
childNodes[0].nodeValue assuming
<div id="preview">&n bsp;</div>

DU
--
The site said to use Internet Explorer 5 or better... so I switched to
Mozilla 1.7.3 :)
Jul 23 '05 #3
>> function callBack() {
if (document.getEl ementById) {
var theDiv =
window.newWindo wReference.docu ment.getElement ById('preview') ;


DU
--
The site said to use Internet Explorer 5 or better... so I switched to
Mozilla 1.7.3 :)


Thanks very much, but I get an "object expected" error in the line
"if(document.ge tElementById).. ." in the newly opened window. Any idea what
may cause this?

Christian
Jul 23 '05 #4
DU wrote:
It's a bad idea to do:

var newWindowRefere nce = window.open(... );
newWindowRefere nce.document.wr ite(...); // or


Don't you have to open the document and close it? Like
newWindowRefere nce.document.op en();
newWindowRefere nce.document.wr ite(...);
newWindowRefere nce.document.cl ose();
" Closes a document stream opened by open() and forces rendering."
http://www.w3c.org/TR/2003/REC-DOM-L...ml#ID-98948567
There is a problem when not closing the document in Mozilla-based
browsers and possibly other browsers as well.


It's still a bad idea for the reason I gave. Attempting to do:

newWindowRefere nce = window.open(... );
// newWindowRefere nce.document may not exist yet!
newWindowRefere nce.document.op en();
newWindowRefere nce.document.wr ite(...);
newWindowRefere nce.document.cl ose();

will fail at least some of the time, in some browsers.

newWindowRefere nce = window.open('ja vascript:opener .someString', ...);

is a better and more reliable way to do this. Of course, the reliability of any of this
depends on JavaScript being enabled, the implementation supporting window.open(), any
popup blocking mechanism the end-user may have and where and how the new window appears
(as a tab in a tabbed browser window?). But, even given all that, do you want to make
it even _more_ unreliable by attempting to access a not-yet-created document of the new
window?
window.newWindo wReference = window.open(... );


newWindowRefere nce is already a type window (return value of the
window.open() call); so it is not needed to prefix it with window.


I included it to indicate that newWindowRefere nce is explicitly a member of the current
window. I prefer to do this rather than having variable assignments that may or may not
be "global" depending on whether it was declared with "var" somewhere earlier in the
same code block.
function callBack() {
if (document.getEl ementById) {
var theDiv =
window.newWindo wReference.docu ment.getElement ById('preview') ;
if (typeof theDiv.innerHTM L == 'string') {


The problem I see here (I might be wrong) is that *as written*, the
innerHTML value of that div id="preview" will be null: there is no
string when writing
<div id="preview"></div>.
Generally speaking, the structure
<div id="preview">&n bsp;</div>
is more manageable from a script perspective.


typeof theDiv.innerHTM L will always be "string" if innerHTML is supported. In your
example (an empty <div>):

<body onload="test(); ">
<div id="preview"></div>.
<script type="text/javascript">
function test() {
var p = document.getEle mentById('previ ew');
alert('[' + p.innerHTML + '];' + typeof p.innerHTML);
}
</script>

IE 6, Mozilla 1.0.2, Mozilla 1.7.3, Firefox 1.0PR, Opera 7.54 show an empty string and
typeof "string". Although I will acknowledge that there may exist some user agents that
support the setting and retrieving of innerHTML, where an empty element returns null
and typeof "object".
theDiv.innerHTM L = 'some text';


Also possible is branching DOM 3 textContent and innerText and/or
childNodes[0].nodeValue assuming
<div id="preview">&n bsp;</div>


The problem I have with the W3C standard is that constructing a node tree for even
mildly complex HTML is extremely tedious and fraught with peril. If you have a string
containing unknown HTML that is moderately to highly complex, innerHTML seems the way
to go. Hand the string to the browser, let it render it. Parsing it (probably badly)
and attempting to construct a node tree out of it is simply duplicating the effort
already put into writing the browser and is better left to the user agent (via
innerHTML). After all, it _knows_ how to do that, it had to do it for the entire page
in the first place.

On the other hand, if I am adding <form> elements or inserting additional fixed, known
content, I typically use the W3C methods.

I really wish innerHTML had been made part of the standard, considering it is probably
extremely easy to implement from the user agent developers' perspective.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq

Jul 23 '05 #5
DU
Grant Wagner wrote:
DU wrote:

It's a bad idea to do:

var newWindowRefere nce = window.open(... );
newWindowRef erence.document .write(...); // or
Don't you have to open the document and close it? Like
newWindowRefe rence.document. open();
newWindowRefe rence.document. write(...);
newWindowRefe rence.document. close();
" Closes a document stream opened by open() and forces rendering."
http://www.w3c.org/TR/2003/REC-DOM-L...ml#ID-98948567
There is a problem when not closing the document in Mozilla-based
browsers and possibly other browsers as well.

It's still a bad idea for the reason I gave. Attempting to do:

newWindowRefere nce = window.open(... );
// newWindowRefere nce.document may not exist yet!


Yes, I agree with you on this. The window may have been created while
the document may not be loaded and referencible in the memory.
newWindowRefere nce.document.op en();
newWindowRefere nce.document.wr ite(...);
newWindowRefere nce.document.cl ose();

will fail at least some of the time, in some browsers.

I agree.
newWindowRefere nce = window.open('ja vascript:opener .someString', ...);

is a better and more reliable way to do this. Of course, the reliability of any of this
depends on JavaScript being enabled, the implementation supporting window.open(), any
popup blocking mechanism the end-user may have and where and how the new window appears
(as a tab in a tabbed browser window?). But, even given all that, do you want to make
it even _more_ unreliable by attempting to access a not-yet-created document of the new
window?

No. Of course not. I was merely indicating that one need to open(), and
then close() the writing of the string into the document. When people
don't do that, the browsers (at least Mozilla-based browsers) are in a
state of perpetual downloading, expecting, waiting for the download to
stop: they are in a state of waiting.
window.newWi ndowReference = window.open(... );
newWindowRefe rence is already a type window (return value of the
window.open () call); so it is not needed to prefix it with window.

I included it to indicate that newWindowRefere nce is explicitly a member of the current
window.


Ok. So, this was for instructional purposes.

I prefer to do this rather than having variable assignments that may or
may not be "global" depending on whether it was declared with "var" somewhere earlier in the
same code block.

Ok.
function callBack() {
if (document.getEl ementById) {
var theDiv =
window.newWi ndowReference.d ocument.getElem entById('previe w');
if (typeof theDiv.innerHTM L == 'string') {
The problem I see here (I might be wrong) is that *as written*, the
innerHTML value of that div id="preview" will be null: there is no
string when writing
<div id="preview"></div>.
Generally speaking, the structure
<div id="preview">&n bsp;</div>
is more manageable from a script perspective.

typeof theDiv.innerHTM L will always be "string" if innerHTML is supported. In your
example (an empty <div>):

<body onload="test(); ">
<div id="preview"></div>.
<script type="text/javascript">
function test() {
var p = document.getEle mentById('previ ew');
alert('[' + p.innerHTML + '];' + typeof p.innerHTML);
}
</script>

IE 6, Mozilla 1.0.2, Mozilla 1.7.3, Firefox 1.0PR, Opera 7.54 show an empty string and
typeof "string". Although I will acknowledge that there may exist some user agents that
support the setting and retrieving of innerHTML, where an empty element returns null
and typeof "object".

theDiv.innerHTM L = 'some text';


Also possible is branching DOM 3 textContent and innerText and/or
childNodes[0].nodeValue assuming
<div id="preview">&n bsp;</div>

The problem I have with the W3C standard is that constructing a node tree for even
mildly complex HTML is extremely tedious and fraught with peril.


Yes, that is fair to say, especially when the node is a document
fragment. But here, it is a simple text node and there are alternatives
(IE-specific and W3C DOM) for such specific case.

If you have a string containing unknown HTML that is moderately to highly complex, innerHTML seems the way
to go. Hand the string to the browser, let it render it. Parsing it (probably badly)
and attempting to construct a node tree out of it is simply duplicating the effort
already put into writing the browser and is better left to the user agent (via
innerHTML). After all, it _knows_ how to do that, it had to do it for the entire page
in the first place.

One negative drawback of this is that users do not learn, understand
what is the DOM, how to handle (create, insert, remove, replace, etc.)
DOM tree node. They hinder their long term understanding of DOM;
eventually (it's already the case IMO) they over-use or mis-use innerHTML.
On the other hand, if I am adding <form> elements or inserting additional fixed, known
content, I typically use the W3C methods.

I really wish innerHTML had been made part of the standard, considering it is probably
extremely easy to implement from the user agent developers' perspective.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq


Mozilla implemented innerHTML for the reasons (simplicity, ease of use)
you give.

DU
--
The site said to use Internet Explorer 5 or better... so I switched to
Mozilla 1.7.3 :)
Jul 23 '05 #6
DU
news.versatel.d e wrote:
function callBack() {
if (document.getEl ementById) {
var theDiv =
window.newWi ndowReference.d ocument.getElem entById('previe w');


DU
--
The site said to use Internet Explorer 5 or better... so I switched to
Mozilla 1.7.3 :)

Thanks very much, but I get an "object expected" error in the line
"if(document.ge tElementById).. ." in the newly opened window. Any idea what
may cause this?

Christian


We already covered and explained this. The new (popup) window maybe
created while the document which is supposed to be in it may not be
entirely loaded (with pointers referencing it in the memory heap) when
you're attempting to write into it. Asynchronous phenomenon. Grant gave
a good explanation and workaround for that:
[
There is no guarantee that the new window is open, or has a valid
document by the time the code referring to it execute. So, for starters,
printview.php should have <body onload="if (opener && opener.callBack )
opener.callBack ();"> as a minimum.
]

DU
--
The site said to use Internet Explorer 5 or better... so I switched to
Mozilla 1.7.3 :)
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2122
by: exolon | last post by:
Yes is it possible to display a layer above an other frame than the one the layer come from ?!? thanks ;)
1
1489
by: Question | last post by:
I am making a custom small menu which is most basic. Problem is that I can't make the first step work for some reason. I have an image to the left of where would be the layer positioned. This layer must be 15 pixels to the left of that image. Since my main page is centered in the browser's window, the pixels to the left of my image are different for each browser and also depends on how wide the browser window is.
1
1942
by: PAD | last post by:
I have written a javascript routine that populates a parent document with a series of <iframe>s. Each <iframe> receives its contents from a series of separate smaller HTML files. Of course my NN 4.8 browser doesn't deal well with the <iframe> so I wrapped the code that generates the <iframe> in <ilayer> tags. NN now recognizes the contents. However within each of the 'content' files I have another javascript routine that basically...
10
8797
by: Peter Altenberg | last post by:
is there some way to make the positioning of a layer (div) so that it is centered in the window. so even when you resize the window it stays centered? i would like to do this with CSS just like you can with tables. thanks, ~peter
1
7031
by: vince.guzman | last post by:
Need help with showing Layer 4 after menu times out onmouseout _______________________________________________ <html> <head> <script language="JavaScript"> <!-- function mmLoadMenus() { if (window.mm_menu_0422101100_0) return;
4
1835
by: jsreblev | last post by:
Hi Javascript gurus: I have tried several things, using several Javascript books, but can't get this to work in Netscape (latest version) and Firefox (latest v.). It works in MIE and Opera fine. A box (defined by a css layer) is supposed to pop out to the right when the corresponding box on the far left is moused over. The first function defines a layer id to the next function which pops out the box. After debugging in Venkman, it...
8
1763
by: nescio | last post by:
hello, i have a script that finds the x and y from the mouse position, this works fine. but now i have an image in a layer : <div id='someLayer'> <img src='someImage.jpg'> </div>
2
2564
by: JS | last post by:
Hi all, Im having some troubles with layers on a site im developing. I have two layers one has an image of an artists palette on it and each blob of paint is a link to a page on the site. That uses an image map and hrefs. The second layer is where the page will appear. So when a user clicks a link on the palette image, a new 'window' will appear over the palette to show the content. The image of the palette will be slightly faded out to...
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.