Get opener element from a child

G

geotso

Hi

How do I get the contents of an element in the opener and show them in the
child?
What I'm trying to do is something like this:

In the opener: <h1 id="pgtitle">Office XP</h1>

In the child:
function gt_pageCont () {
var pagetitle = window.opener.document.getElementById("pgtitle");
var pagetext = "<h1>\""+title+"\"</h1><p>Here are some more info about \""
+title+ "\".</p>";
document.write(pagetext);
}

and:
<body onload="gt_pageCont();">

I know that something is missing in the function, because I've tested it and
the child displays [Object] instead of "Office XP". Can someone help me
please?

Thanks.
 
D

Daniel Kirsch

geotso said:
function gt_pageCont () {
var pagetitle = window.opener.document.getElementById("pgtitle");
var pagetext = "<h1>\""+title+"\"</h1><p>Here are some more info about \""
+title+ "\".</p>";
document.write(pagetext);
}

var pagetitle = opener.document.getElementById("pgtitle").innerHTML;
var pagetext = "<h1>\""+pagetitle+"\"<\/h1><p>Here are some more info
about \""
+pagetitle+ "\".<\/p>";
document.write(pagetext);
}

Instead of using document.write() you may also set an element's
innerHTML property accordingly.

Daniel
 
R

RobG

Daniel Kirsch wrote:
[...]
var pagetitle = opener.document.getElementById("pgtitle").innerHTML;

You could also use:

opener.document.getElementById('pgtitle').firstChild.data;

But both methods have the restriction that if there is anything in the
pgtitle element other than plain text, you will not get what you want.

e.g. if you add extra markup inside the element, the innerHTML method
will return the extra markup along with the text and the firstChild
method will do different things depending on where the markup is.

e.g.
<h1 id="pgtitle">Office <span
style="font-weight: normal;">XP</span></h1>

will return "Office "

<h1 id="pgtitle"><span
style="font-weight: normal;">Office XP</span></h1>

will return "undefined".

Which highlights that using the document content as a source of data is
not always reliable, particularly when HTML munges markup and content
so thoroughly. Maybe you should consider putting the pgtitle value
somewhere else where it will not be affected by markup - say a global
variable for the opener page or a meta tag? That should mean you don't
need to parse whatever is returned to ensure it is what you want.

If you stick with the innerHTML method, you probably should use a
regular expression to remove any markup between tag delimiters.

Or have I been rambling for no particular purpose...
 
D

Daniel Kirsch

RobG said:
But both methods have the restriction that if there is anything in the
pgtitle element other than plain text, you will not get what you want.

innerHTML returns the original HTML. If you just want to get the text
content without markup, one might use innerText (IE) or textContent
(newer Geckos) or of course a regExp on the innerHTML value.

Daniel
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top