[javascript / Safari] getSelection() Workaround werkt niet.

Pagina: 1
Acties:

  • Collaborator
  • Registratie: April 2002
  • Laatst online: 30-03 11:49

Collaborator

You! Cake or Death?

Topicstarter
Zoals de titel al duidelijk maakt werkt het pakken van de selectie niet in safari (wel in IE5/IE6/Firefox)
Het is de bedoeling dat er tags voor en achter een selectie geplaats worden in een textveld (zoals ook op GoT bij de "post reply").
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<script type="text/javascript">
// globals
var curSelVal = "";
var startPos = 0;
var endPos = 0;

// functions
function storeCurVal(obj, start, end) {
   // function not called if IE 4+
     startPos = obj.selectionStart;
   endPos = obj.selectionEnd;
   curSelVal = document.forms[0].copy.value;
}

function getSel(tag)
{   
  if(document.selection){
        oldString = document.selection.createRange().text;
      if (oldString != "") {
         document.selection.createRange().text = '<' + tag + '>' + oldString + '</' + tag + '>';
      }
       return false;
  } else if(document.getSelection){
      oldString = curSelVal;
      newString = '';
      finalString = '';
      len = curSelVal.length; // length of current string
      if (len > 0 && document.forms[0].copy.value.length > 0) {
         // get from beginning of string to start position
         firstPart = oldString.substring(0, startPos);
         // get from end of selection to end of total string
         lastPart = oldString.substring(endPos, len);
   
         // store the new string
         for (i=startPos; i<endPos; i++) {
            newString += oldString[i];
         }
         // surround it with the proper tag
         finalString = '<' + tag + '>' + newString + '</' + tag + '>';
   
         // rewrite it back into the textarea
         document.forms[0].copy.value = firstPart + finalString + lastPart;
      } else {
         curSelVal = "";
      }
  } else if(document.getSelection){ // don't know which browser would actually call this
         txt = obj.setSelectionRange(obj.selectionStart, obj.selectionEnd);
  }
 
}
</script>


Na wat zoeken kwam ik er op uit dat Safari anders met de selectie omgaat, en dat het dus anders moet.

zie
http://www.quirksmode.org/js/selected.html
en
http://joemaller.com/2005...3/getselection-workaround

Daar staat als dit als fix:
Update: After working through all of the above, I realized there is a far simpler solution: +''. The Safari problem seems to be that string methods do not work on the returned object from getSelection(). Forcing the result into a string by concatenating with an empty string fixes all of my bookmarklets. Concat() fails because it’s a method of string, use the "+" joining operator and an empty string '' instead.

* getSelection() Faster Fix
javascript:d=window.getSelection()+'';
d=(d.length==0)?document.title:d;
alert(d);

Joe at
Maar dat werkt dus niet in mijn geval, of doe ik nou iets fout?

Mijn testje staat hier online:

http://www.konoha.nl/lp/index3.html

This space is occupied


  • whoami
  • Registratie: December 2000
  • Laatst online: 00:06
javascript hoort thuis in w&g

p&w->w&g

https://fgheysels.github.io/