sentinel wrote:
Do you know of a solution that will work on Netscape 7.1 ?
Netscape doesn't implement the document.select ion object that IE has,
nor the TextRange that IE has.
It has a buggy implementation of the W3C DOM Level 2 Range specification
documented at
http://www.w3.org/TR/DOM-Level-2-Tra...ge/ranges.html
which is connected to its selection object returned by
window.getSelec tion()
whose interface is documented at
http://lxr.mozilla.org/seamonkey/sou...ISelection.idl
Theoretically you should be able to use a method like surroundContent s:
http://www.w3.org/TR/DOM-Level-2-Tra...ge-Surrounding
to wrap some range content into a span element for instance but last
time I have tried that with Netscape 7 it failed due to bugs.
When I try the following with Mozilla 1.7 or FireFox 0.9
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>does surroundContent s work?</title>
<script type="text/javascript">
function wrapSelection (cssClassName) {
if (typeof window.getSelec tion != 'undefined') {
var selection = window.getSelec tion();
if (typeof selection.range Count != 'undefined' &&
selection.range Count > 0) {
var range = selection.getRa ngeAt(0);
if (typeof range.surroundC ontents != 'undefined') {
var span = document.create Element('span') ;
span.className = cssClassName;
range.surroundC ontents(span);
}
}
}
}
</script>
<style type="text/javascript">
..selectionHigh light {
background-color: lightyellow;
}
</style>
</head>
<body>
<p>
Select some text in this paragraph with the mouse.
Then press the highlight button.
All for Kibology. Kibology for all.
</p>
<form action="" onsubmit="retur n false;">
<p>
<input type="button" value="highligh t selection"
onclick="wrapSe lection('select ionHighlight'); ">
</p>
</form>
</body>
</html>
I still get an error on the surroundContent s call:
Error: uncaught exception: [Exception... "Index or size is negative or
greater than the allowed amount" code: "1" nsresult: "0x80530001
(NS_ERROR_DOM_I NDEX_SIZE_ERR)" location:
"http://localhost/javascript/test20040623.ht ml Line: 15"]
which I think is caused by the buggy Mozilla implementation of
Range/surroundContent s.
--
Martin Honnen
http://JavaScript.FAQTs.com/