J
Justin Koivisto
I am having a problem with Internet Explorer and javascript on a WYSIWYG
editor I am creating. It all works in Mozilla, but IE returns flase for
queryCommandEnabled('forecol'). JS isn't my strong suit, so I am turning
to all you experts for help. What am I missing here? Other commands like
formatblock, fontsize and fontname all work fine, so I assume that it
has something to do with not getting the correct selection because of
the iframe. Any help is greatly appreciated.
In an HTML document I have the following line:
<img src="images/forecol.png" onClick="DoTextFormat('forecolor','')"
id="forecolor">
<iframe width="175" height="125" id="forecolor0" src="palette.html"
style="visibility: hidden; position: absolute; left: 0px; top:
0px;"></iframe>
palette.html is like this:
<html>
<head>
<title>Color Palete</title>
<script language="JavaScript" type="text/javascript">
<!--
function selectColor(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.onclick = click;
}
}
function click(){
str=this.id;
color=str.replace('c','#');
selectColor(color);
}
-->
</script>
<style type="text/css">
<!--
html,body{margin: 0px; padding: 0px;}
td{border: black none 2px;}
td:hover{border: white solid 2px;}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body bgcolor="white" onLoad="InitColorPalette()">
<table border="0" cellpadding="0" cellspacing="2">
<tr>
<td id="cFFFFFF" bgcolor="#FFFFFF" width="15" height="15"> </td>
<td id="cFFCCCC" bgcolor="#FFCCCC" width="15" height="15"> </td>
....
In the included javascript source file I have the following functions:
function DoTextFormat(command, option){
if((command=='forecolor') || (command=='hilitecolor')){
parent.command=command;
layerid=command+'0';
buttonElement=document.getElementById(command);
document.getElementById(layerid).style.left=getOffsetLeft(buttonElement)
+ 'px';
document.getElementById(layerid).style.top=(getOffsetTop(buttonElement)
+ buttonElement.offsetHeight) + 'px';
if(document.getElementById(layerid).style.visibility=='hidden')
document.getElementById(layerid).style.visibility='visible';
else{
document.getElementById(layerid).style.visibility='hidden';
}
//get current selected range
var
sel=document.getElementById('iView').contentWindow.document.selection;
if(sel!=null){
rng=sel.createRange();
}
}else{
if(document.getElementById('iView').contentWindow.document.queryCommandEnabled(command)){
document.getElementById('iView').contentWindow.document.execCommand(command,
false, option);
return true;
}else return false;
}
document.getElementById('iView').contentWindow.focus();
}
function setColor(color){
elem=command+'0';
if(browser.isIE5up){
//retrieve selected range
var
sel=document.getElementById('iView').contentWindow.document.selection;
if(sel!=null){
var newselectionRange=sel.createRange();
newselectionRange.select();
}
}else{
document.getElementById('iView').contentWindow.focus();
}
if(document.getElementById('iView').contentWindow.document.queryCommandEnabled(command)){
document.getElementById('iView').contentWindow.document.execCommand(command,
false, color);
}else{
alert("queryCommandEnabled("+command+") returned false. Cannot set color
to "+color);
// close palette before exiting function
document.getElementById(elem).style.visibility='hidden';
return false;
}
document.getElementById('iView').contentWindow.focus();
document.getElementById(elem).style.visibility='hidden';
return true;
}
TIA
editor I am creating. It all works in Mozilla, but IE returns flase for
queryCommandEnabled('forecol'). JS isn't my strong suit, so I am turning
to all you experts for help. What am I missing here? Other commands like
formatblock, fontsize and fontname all work fine, so I assume that it
has something to do with not getting the correct selection because of
the iframe. Any help is greatly appreciated.
In an HTML document I have the following line:
<img src="images/forecol.png" onClick="DoTextFormat('forecolor','')"
id="forecolor">
<iframe width="175" height="125" id="forecolor0" src="palette.html"
style="visibility: hidden; position: absolute; left: 0px; top:
0px;"></iframe>
palette.html is like this:
<html>
<head>
<title>Color Palete</title>
<script language="JavaScript" type="text/javascript">
<!--
function selectColor(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.onclick = click;
}
}
function click(){
str=this.id;
color=str.replace('c','#');
selectColor(color);
}
-->
</script>
<style type="text/css">
<!--
html,body{margin: 0px; padding: 0px;}
td{border: black none 2px;}
td:hover{border: white solid 2px;}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body bgcolor="white" onLoad="InitColorPalette()">
<table border="0" cellpadding="0" cellspacing="2">
<tr>
<td id="cFFFFFF" bgcolor="#FFFFFF" width="15" height="15"> </td>
<td id="cFFCCCC" bgcolor="#FFCCCC" width="15" height="15"> </td>
....
In the included javascript source file I have the following functions:
function DoTextFormat(command, option){
if((command=='forecolor') || (command=='hilitecolor')){
parent.command=command;
layerid=command+'0';
buttonElement=document.getElementById(command);
document.getElementById(layerid).style.left=getOffsetLeft(buttonElement)
+ 'px';
document.getElementById(layerid).style.top=(getOffsetTop(buttonElement)
+ buttonElement.offsetHeight) + 'px';
if(document.getElementById(layerid).style.visibility=='hidden')
document.getElementById(layerid).style.visibility='visible';
else{
document.getElementById(layerid).style.visibility='hidden';
}
//get current selected range
var
sel=document.getElementById('iView').contentWindow.document.selection;
if(sel!=null){
rng=sel.createRange();
}
}else{
if(document.getElementById('iView').contentWindow.document.queryCommandEnabled(command)){
document.getElementById('iView').contentWindow.document.execCommand(command,
false, option);
return true;
}else return false;
}
document.getElementById('iView').contentWindow.focus();
}
function setColor(color){
elem=command+'0';
if(browser.isIE5up){
//retrieve selected range
var
sel=document.getElementById('iView').contentWindow.document.selection;
if(sel!=null){
var newselectionRange=sel.createRange();
newselectionRange.select();
}
}else{
document.getElementById('iView').contentWindow.focus();
}
if(document.getElementById('iView').contentWindow.document.queryCommandEnabled(command)){
document.getElementById('iView').contentWindow.document.execCommand(command,
false, color);
}else{
alert("queryCommandEnabled("+command+") returned false. Cannot set color
to "+color);
// close palette before exiting function
document.getElementById(elem).style.visibility='hidden';
return false;
}
document.getElementById('iView').contentWindow.focus();
document.getElementById(elem).style.visibility='hidden';
return true;
}
TIA