Solved

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

Posted on 2003-11-12
17
2,307 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:wjdashwood
  • 9
  • 8
17 Comments
 
LVL 10

Expert Comment

by:NetGroove
ID: 9734245
Is the element with the id "edit" also an iframe?

0
 

Author Comment

by:wjdashwood
ID: 9734913
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
 
LVL 10

Expert Comment

by:NetGroove
ID: 9737794
This was from my proposal in your previous question.

Paste here your complette source for analysis.

0
 

Author Comment

by:wjdashwood
ID: 9738805
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
 
LVL 10

Expert Comment

by:NetGroove
ID: 9739536
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
 

Author Comment

by:wjdashwood
ID: 9739867
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
 
LVL 10

Expert Comment

by:NetGroove
ID: 9739982

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

To this format:
var sel = document.selection;

0
 

Author Comment

by:wjdashwood
ID: 9740344
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 10

Expert Comment

by:NetGroove
ID: 9740743
Send me the complete page to netgroovy@yahoo.com
0
 

Author Comment

by:wjdashwood
ID: 9740806
thanks
0
 

Author Comment

by:wjdashwood
ID: 9765469
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
 
LVL 10

Accepted Solution

by:
NetGroove earned 0 total points
ID: 9765568
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
 

Author Comment

by:wjdashwood
ID: 9778862
Thank you very much. This is really apreciated.
0
 

Author Comment

by:wjdashwood
ID: 9809943
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
 

Author Comment

by:wjdashwood
ID: 9837155
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
 
LVL 10

Expert Comment

by:NetGroove
ID: 9837542
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
 
LVL 10

Expert Comment

by:NetGroove
ID: 9876641
See your new question.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now