admin管理员组文章数量:1397142
The script below will replace selected word in a textarea. But it only works on IE. Any idea how to make it work on Firefox? (The problem seems to lie in (document.all)? document.selection.createRange() : document.getSelection();)
<SCRIPT LANGUAGE="JavaScript">
<!--//
var seltext = null;
var repltext = null;
function replaceit()
{
seltext = (document.all)? document.selection.createRange() : document.getSelection();
var selit = (document.all)? document.selection.createRange().text : document.getSelection();
if (selit.length>=1){
if (seltext) {
repltext= prompt('Please enter the word to replace:', ' ');
if ((repltext==' ')||(repltext==null)) repltext=seltext.text;
seltext.text = repltext;
window.focus()
}
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<form name="f">
<textarea cols='40' rows='10' name='msg'></textarea>
<input type="button" name="b" value="Replace" onClick="replaceit();">
</form>
</BODY>
The script below will replace selected word in a textarea. But it only works on IE. Any idea how to make it work on Firefox? (The problem seems to lie in (document.all)? document.selection.createRange() : document.getSelection();)
<SCRIPT LANGUAGE="JavaScript">
<!--//
var seltext = null;
var repltext = null;
function replaceit()
{
seltext = (document.all)? document.selection.createRange() : document.getSelection();
var selit = (document.all)? document.selection.createRange().text : document.getSelection();
if (selit.length>=1){
if (seltext) {
repltext= prompt('Please enter the word to replace:', ' ');
if ((repltext==' ')||(repltext==null)) repltext=seltext.text;
seltext.text = repltext;
window.focus()
}
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<form name="f">
<textarea cols='40' rows='10' name='msg'></textarea>
<input type="button" name="b" value="Replace" onClick="replaceit();">
</form>
</BODY>
Share
Improve this question
edited Nov 19, 2011 at 12:15
Emond
50.7k11 gold badges86 silver badges117 bronze badges
asked Apr 15, 2009 at 1:30
John SeeJohn See
4 Answers
Reset to default 2The document.all bit is being used as a test to see if it's IE or not. The way it's written document.getSelection() is what would be used in Firefox and document.selection.createRange() in IE
See http://www.hscripts./tutorials/javascript/ternary.php
So the problem isn't document.all, but rather that the getSelection() isn't working. Not sure why exactly as that's not a construct I've used recently, but try window.getSelection() as per this: (and google for others if this doesn't do the trick) http://www.webdeveloper./forum/archive/index.php/t-138944.html
OK, so document.getSelection()
returns a string in FF. String::text does not exist. So you can't set that.
The basc idea of what you need to do (and it'll work in both browsers):
Get the text area by its id
--you'll need to set an id
attribute on the textarea. Get the starting and ending positions of the selection. Then take three substrings: 0->start, start->end, end->string.length. Replace the middle substring with whatever they put in the prompt. Set the text
of the textarea to your newly formed string.
The exact how is up to you, I just gave you a flavor of the procedure.
My O'Reilly flamingo book is at work, but I seem to remember reading the document.getSelection() specifically does not work with textarea elements, only the "non-editable" parts of the page.
Window.getSelection is the method that makes a DOMSelection object available in firefox. Perhaps this is what you are looking for.
本文标签: cross browserJavascript documentall and documentgetSelectionFirefox alternativeStack Overflow
版权声明:本文标题:cross browser - Javascript document.all and document.getSelection - Firefox alternative - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744130199a2592141.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论