Document.selection.createRange() not working in chrome and safari?

There are a number of foibles when getting selected text in a document, mostly related to whether or not text is selected in a form control or as text of some other element. Try a function that does something like: function checkForSelectedText(e) { var e = e || window. Event; var el = e.

Target || e. SrcElement; var tagName = el. TagName && el.tagName.toLowerCase(); var t; var d = document; // Try DOM 2 Range - for most browsers, including IE 6+ // However, doesn't get text selected inside form controls // that allow selection of text content (input type text, // textarea) if (d && d.

Selection && d.selection. CreateRange) { t = d.selection.createRange(). Text; // Otherwise try HTML5 - note that getSelection returns // a string with extra properties.

This may also get // text within input and textarea } else if (d. GetSelection) { t = d.getSelection(); } // If didn't get any text, see if event was inside // inupt@type=text or textarea and look for text if (t == '') { if (tagName == 'textarea' || (tagName == 'input' && el. Type == 'text')) { // Check selectionStart/End as otherwise if no text // selected, IE returns entire text of element if (typeof el.

SelectionStart == 'number' && el. SelectionStart! = el.

SelectionEnd) { t = el.value. Substring(el. SelectionStart, el.

SelectionEnd) } } } return t; }.

In non IE (excluding IE9) browsers (see comments), use window. GetSelection to get the selection object. In IE Function GetSelection () { if (window.

GetSelection) { // all browsers, except IE before version 9 var selectionRange = window. GetSelection (); return selectionRange.toString(); } else { if (document.selection. Type == 'None') { return ""; } else { var textRange = document.selection.

CreateRange (); return textRange. Text; } } } function DoPaste(control) { maxLength = control. Attributes"maxlength".

Value; if (maxLength) { var oTR = GetSelection(); } } In general, working with selection and ranges is very tricky as browser supports varies so much. Here's an excellent reference which lists out browser support (and what code works where) and sample code that works in corresponding browsers: http://help.dottoro.com/ljefwsqm.php.

When I give window. GetSelection in IE it shows Undefined – Mathew Paul Aug 11 at 6:17 In IE, your original code will work. If you check the reference, it tells you what code is supported in what browser.

I find it very helpful and they have sample cross broswer code too. – Mrchief Aug 11 at 6:19 Opera 10.5 supports window.getSelection() too. What distinction is there between Opera pre and post 10.5?

– Tim Down Aug 11 at 10:58 @Tim Down: Are you sure? Usually dottoro is right about the browser support thing. But anyway, I would check for if(window.

GetSelection) and not any specific browser version so if its there, the code would work. – Mrchief Aug 11 at 13:57 @Mrchief: Yes, I'm sure. What I think has confused things is that according to dottoro, they removed document.

Selection in 10.5, but it definitely had window.getSelection() before that (I'm certain some version 9. X had it), so there was overlap. See help.dottoro.Com/ljcvonpc.

Php – Tim Down Aug 11 at 16:20.

I would use JQuery to do the same because it's cross browser and you write more abstract java script instead of the native browser specific one your wrote so far.

This should be a comment anyway (unless improved upon). – Mrchief Aug 11 at 6:13 1 Best jquery function for selecting text: stackoverflow.Com/questions/985272/… – drharris Aug 11 at 6:18 1 @drharris - that function uses browser sniffing so if you turn up with a browser other than the 4 sniffed for, bad luck. – RobG Aug 11 at 6:25 The answer below it mods it to use null checks instead of browser sniffing.

– drharris Aug 11 at 6:27 Selecting text is always not the same as working with ranges. – Mrchief Aug 11 at 6:30.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions