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

Retrieving section of HTML from within an IFRAME

P: n/a
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
Share this Question
Share on Google+
2 Replies


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

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