469,354 Members | 2,035 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Retrieving section of HTML from within an IFRAME

Hi,

What I am trying to do is to work out which part of a HTML page contained
within an IFRAME has been selected. I have complete control over both the
main window and the content of the IFRAME and they are both on the same
domain. I also have the luxury of saying work in IE5+ only.

This is kind of what I want to achieve, but obviously is not working
functioning code:

content.htm:
<body>
<span id="area01">This is the text in area 1</span><br />
<span id="area02"><b>This is the text in area 2</b></span><br />
</body>

frame.htm:
<body>
<script language="javascript">
function displaySection()
{
var range =
document.frames["iframe1"].document.selection.createRange();
var el = range.parentElement();
alert(el.tagName);
}
</script>
<form name="form1" method="post">
<input type="button" value="Get the iframe section"
onClick='displaySection();'>
</form>
<iframe name="iframe1" src="http://localhost/content.htm" height="400"
width="600"></iframe>
</body>

What I want to be able to do is always get a surrounding SPAN/DIV/(whatever
I choose or am told to group the section by) rather than the nearest
parentElement, as telling me that some selected text is bold gives me no
indication as to whether the text is contained within area01 or area02.

Any help much appreciated.

Regards,
Larry.

btw if you must reply to me rather than the group its lneylon AT ntlworld
DOT com.
Jul 23 '05 #1
2 1729
Ivo
"Larry Neylon" wrote
<span id="area01">This is the text in area 1</span><br />
<span id="area02"><b>This is the text in area 2</b></span><br /> <snip> function displaySection()
{
var range =
document.frames["iframe1"].document.selection.createRange();
var el = range.parentElement();
alert(el.tagName);
}
</script> <snip> What I want to be able to do is always get a surrounding SPAN/DIV/(whatever I choose or am told to group the section by) rather than the nearest
parentElement, as telling me that some selected text is bold gives me no
indication as to whether the text is contained within area01 or area02.


Add the following two lines just before where the alert is in your code:
while (el.tagName!='BODY' && !/area\d+/i.test(el.id) )
el=el.parentElement();

This means as long as el's id is not "area[number]", it will look to the
next containing element.
HTH
Ivo
Jul 23 '05 #2
Cheers Ivo,

You are indeed a wise man. Worked just as I wanted.

Larry
"Ivo" <no@thank.you> wrote in message
news:40***********************@news.wanadoo.nl...
"Larry Neylon" wrote
<span id="area01">This is the text in area 1</span><br />
<span id="area02"><b>This is the text in area 2</b></span><br />

<snip>
function displaySection()
{
var range =
document.frames["iframe1"].document.selection.createRange();
var el = range.parentElement();
alert(el.tagName);
}
</script>

<snip>
What I want to be able to do is always get a surrounding

SPAN/DIV/(whatever
I choose or am told to group the section by) rather than the nearest
parentElement, as telling me that some selected text is bold gives me no
indication as to whether the text is contained within area01 or area02.


Add the following two lines just before where the alert is in your code:
while (el.tagName!='BODY' && !/area\d+/i.test(el.id) )
el=el.parentElement();

This means as long as el's id is not "area[number]", it will look to the
next containing element.
HTH
Ivo

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by William Starr Moake | last post: by
2 posts views Thread by Toni | last post: by
28 posts views Thread by Christopher Benson-Manica | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.