1 | var theSelection = false; |
---|
2 | |
---|
3 | function SmiliesWrite(form, field, start, end, force) { |
---|
4 | var textarea = document.forms[form].elements[field]; |
---|
5 | |
---|
6 | storeCaret(textarea); |
---|
7 | |
---|
8 | if (textarea.caretPos) { |
---|
9 | textarea.focus(); |
---|
10 | // Attempt to create a text range (IE). |
---|
11 | theSelection = document.selection.createRange().text; |
---|
12 | if (force || theSelection != '') { |
---|
13 | document.selection.createRange().text = start + theSelection + end; |
---|
14 | textarea.focus(); |
---|
15 | return true; |
---|
16 | } |
---|
17 | } else if (typeof(textarea.selectionStart) != "undefined") { |
---|
18 | // Mozilla text range replace. |
---|
19 | var text = new Array(); |
---|
20 | text[0] = textarea.value.substr(0, textarea.selectionStart); |
---|
21 | text[1] = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd-textarea.selectionStart); |
---|
22 | text[2] = textarea.value.substr(textarea.selectionEnd); |
---|
23 | caretPos = textarea.selectionEnd+start.length+end.length; |
---|
24 | if (force || text[1] != '') { |
---|
25 | textarea.value = text[0]+start+text[1]+end+text[2]; |
---|
26 | if (textarea.setSelectionRange) { |
---|
27 | textarea.focus(); |
---|
28 | textarea.setSelectionRange(caretPos, caretPos); |
---|
29 | } |
---|
30 | return true; |
---|
31 | } |
---|
32 | } else if (force) { |
---|
33 | // Just put it on the end. |
---|
34 | textarea.value += start+end; |
---|
35 | textarea.focus(textarea.value.length-1); |
---|
36 | return true; |
---|
37 | } |
---|
38 | return false; |
---|
39 | } |
---|
40 | |
---|
41 | function storeCaret(text) { |
---|
42 | if (text.createTextRange) text.caretPos = document.selection.createRange().duplicate(); |
---|
43 | } |
---|