error in code - 'document.getElementById(...).contentWindow.document.selection'

I'm struggling locating the problem with some code. In IE it comes up with the error:
'document.getElementById(...).contentWindow.document.selection' is null or not an object

here is the function it is tripping up on:
function EditPage(command, option) {
if (command == "forecolor") {
            parent.command = command;
            buttonElement = document.getElementById(command);
            document.getElementById("colorpalette").style.left = getOffsetLeft(buttonElement) + "px";
            document.getElementById("colorpalette").style.top = (getOffsetTop(buttonElement) + buttonElement.offsetHeight) + "px";
            if (document.getElementById("colorpalette").style.visibility == "hidden")
                  document.getElementById("colorpalette").style.visibility="visible";
            else {
                  document.getElementById("colorpalette").style.visibility="hidden";
            }
            //get current selected range
            var sel = document.getElementById("edit").contentWindow.document.selection;
            if (sel!=null) {
                  rng = sel.createRange();
            }
      forecolor.src='images/buttons/textcolour.gif';
      }
else{
        document.getElementById("edit").focus();
        document.execCommand(command);
        document.getElementById("edit").focus();
}
}

this is a rich text box which has a button calling up this function with "forecolor" as the id.  The colorpalette is an iframe in the code which brings up a colour palette at the correct position.

When I run the code the palette appears when the button is pressed and that error occurs, whenever I click in the colour palette it will not change the colour of the text highlighted.

Thanks


www.d6online.co.uk
wjdashwoodAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NetGrooveCommented:
Is the element with the id "edit" also an iframe?

0
wjdashwoodAuthor Commented:
no, its just part of a table. The ID is not actually edit though, the ID is forecolor but that is what I used for bold italic etc so I assumed it would work with this. Also thats what someone else had in some code I have been using to try and help me.
0
NetGrooveCommented:
This was from my proposal in your previous question.

Paste here your complette source for analysis.

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

wjdashwoodAuthor Commented:
Yes the bold code was what you provided in a previous question. I aslo added to it others like underline, italic, undo, cut, copy and paste ect alosing with a large amount of formatting so the code is now many pages long. The code i'm putting here is anything to do with the colour changing, the others should not affect it in any way but there is too much to paste here.


This is the line of code which when clicked on should start up the colour changing procedure:
<tr><td><a class="text" href="javascript:" onClick="EditPage('forecolor'),textcolour.src='images/buttons/stextcolour.gif'" onmouseover="setHover(forecolor)">&nbsp;&nbsp;&nbsp;<img id="forecolor" name="textcolour" border=0 src="images/buttons/textcolour.gif">&nbsp;&nbsp;Change the selected text colour</a> </td></tr>



This is the rich text box and the colorpalette iframe.
<div id="edit" onkeyup="reset(); markSelectedElement();" contenteditable="true" style="OVERFLOW: auto; WIDTH: 100%; HEIGHT: 100%" onclick="reset(); markSelectedElement()"  onmouseup="markSelectedElement();" height="100%" width="100%">
</div><iframe width="227" height="168" id="colorpalette" src="palette.php" style="visibility:hidden; position: absolute; left: 0px; top: 0px;"></iframe>

these are the functions from the palette.php:
function selectColour(color)
{
      self.parent.setColor(color);
}

function InitColorPalette() {
      if (document.getElementsByTagName)
            var x = document.getElementsByTagName('TD');
      else if (document.all)
            var x = document.all.tags('TD');
      for (var i=0;i<x.length;i++) {
            x[i].onmouseover = over;
            x[i].onmouseout = out;
            x[i].onclick = click;
      }
}

function over() {
      this.style.border='1px dotted white';
}

function out() {
      this.style.border='1px solid gray';
}

function click() {
      selectColour(this.id);
}

they are followed by a huge table, example line:
<td id="#FFFFFF" bgcolor="#FFFFFF" width="20" height="20"><img width="1" height="1"></td>

I hope this is enough to make sense of.

0
NetGrooveCommented:
Your error is araising from this line:

var sel = document.getElementById("edit").contentWindow.document.selection;

Therefore you have to search in your HTML code for an element with the ID "edit"
Find that html tag with the id "edit" and paste its html code here.

0
wjdashwoodAuthor Commented:
The rich text box has the edit id
<div id="edit" onkeyup="reset(); markSelectedElement();" contenteditable="true" style="OVERFLOW: auto; WIDTH: 100%; HEIGHT: 100%" onclick="reset(); markSelectedElement()"  onmouseup="markSelectedElement();" height="100%" width="100%">
</div>
0
NetGrooveCommented:

Rrewrite this line:
var sel = document.getElementById("edit").contentWindow.document.selection;

To this format:
var sel = document.selection;

0
wjdashwoodAuthor Commented:
How I don't get the immediate error, but when I click on a colour it says object doesn't support this property or method.
0
NetGrooveCommented:
Send me the complete page to netgroovy@yahoo.com
0
wjdashwoodAuthor Commented:
thanks
0
wjdashwoodAuthor Commented:
is it something to do with the iframe, which is now a div tag? In the exapmle I was working from they had an iframe not a div tag, could this make a difference?
0
NetGrooveCommented:
Sorry, I missed this.

The change from iframe to div makes it necessary to access the selection from this:
 var sel = document.getElementById("edit").contentWindow.document.selection;

To this:
 var sel = document.selection;

But when this is corrected, it still cannot pass the selected color because there is no such function on the window to pass the color.
Color passing is done by this line in palette.php:
  self.parent.setColor(color);

There is no such function on parent window to be called: setColor()
I will try to create the function tomorrow.

Now I have to leave the office.

See you,
NetGroove

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wjdashwoodAuthor Commented:
Thank you very much. This is really apreciated.
0
wjdashwoodAuthor Commented:
is there not a way of just passing the colour back in a return statement? then using that colour somehow within the normal forecolor function?
0
wjdashwoodAuthor Commented:
perhaps you could explain to me the difference between the div tag and an iframe and why there is a problem so that I could try and figure a solution out myself?
Thanks
H
0
NetGrooveCommented:
You are right, the best way is to enable you to find your solution.

Ok,

the difference is that the div as an element of a document inside a window, and the iframe is a child window of the surrounding window document. You can access the iframe object as every other element on parent window, but the iframe is a new window with own document and own elements.

In your upper problem was the iframe and div difference solved by using the correct selection range access.
For iframe wint the id="edit" it is this line:
         var sel = document.getElementById("edit").contentWindow.document.selection;

And for div it is this line:
         var sel = document.selection;

0
NetGrooveCommented:
See your new question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.