News:

Wondering if this will always be free?  See why free is better.

Main Menu

IE 11 console errors when typing

Started by MobileCS, March 02, 2018, 12:04:27 PM

Previous topic - Next topic

MobileCS

IE 11 is throwing a console error for every letter that is typed in the message box.

SCRIPT5007: Unable to get property 'createRange' of undefined or null reference

scripts/script.js : line 344

// Remember the current position.
function storeCaret(oTextHandle)
{
// Only bother if it will be useful.
if ('createTextRange' in oTextHandle)
---> oTextHandle.caretPos = document.selection.createRange().duplicate();
}




MobileCS

This is how I solved the issue. Not sure if this is the proper way to fix it though.

function storeCaret(oTextHandle)
{
// Only bother if it will be useful.
if ('createTextRange' in oTextHandle)
{
if (document.getSelection)
oTextHandle.caretPos = document.getSelection(); // IE 11
else
oTextHandle.caretPos = document.selection.createRange().duplicate();
}
}

MobileCS

The fix above does not work. I'm not able to insert smileys.

vbgamer45

Community Suite for SMF - Take your forum to the next level built for SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com -  Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

MobileCS

Ok, the following code works for me.

function storeCaret(oTextHandle)
{
// Only bother if it will be useful.
if ('createTextRange' in oTextHandle)
{
if (document.selection != null)
oTextHandle.caretPos = document.selection.createRange().duplicate();
}
}

Chen Zhen

It appears as though IE11 can use some of the same syntax as Mozilla but IE9 and prior can not.
However IE11 is also somewhat backward compatible therefore the initial conditions are making it attempt to use the IE9 syntax.
Try replacing the 3 functions in that file with the following which reverses the first 2 conditions in two of those:

// Remember the current position.
function storeCaret(oTextHandle)
{
// Only bother if it will be useful.
if ('createTextRange' in oTextHandle)
{
if (document.getSelection() == "undefined")
oTextHandle.caretPos = document.selection.createRange().duplicate();
else
oTextHandle.caretPos = document.getSelection();
}
}

// Replaces the currently selected text with the passed text.
function replaceText(text, oTextHandle)
{
// Mozilla text range replace.
if ('selectionStart' in oTextHandle)
{
var begin = oTextHandle.value.substr(0, oTextHandle.selectionStart);
var end = oTextHandle.value.substr(oTextHandle.selectionEnd);
var scrollPos = oTextHandle.scrollTop;

oTextHandle.value = begin + text + end;

if (oTextHandle.setSelectionRange)
{
oTextHandle.focus();
var goForward = is_opera ? text.match(/\n/g).length : 0;
oTextHandle.setSelectionRange(begin.length + text.length + goForward, begin.length + text.length + goForward);
}
oTextHandle.scrollTop = scrollPos;
}
// Attempt to create a text range (IE).
else if ('caretPos' in oTextHandle && 'createTextRange' in oTextHandle)
{
var caretPos = oTextHandle.caretPos;

caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
caretPos.select();
}
// Just put it on the end.
else
{
oTextHandle.value += text;
oTextHandle.focus(oTextHandle.value.length - 1);
}
}

// Surrounds the selected text with text1 and text2.
function surroundText(text1, text2, oTextHandle)
{
// Mozilla text range wrap.
if ('selectionStart' in oTextHandle)
{
var begin = oTextHandle.value.substr(0, oTextHandle.selectionStart);
var selection = oTextHandle.value.substr(oTextHandle.selectionStart, oTextHandle.selectionEnd - oTextHandle.selectionStart);
var end = oTextHandle.value.substr(oTextHandle.selectionEnd);
var newCursorPos = oTextHandle.selectionStart;
var scrollPos = oTextHandle.scrollTop;

oTextHandle.value = begin + text1 + selection + text2 + end;

if (oTextHandle.setSelectionRange)
{
var goForward = is_opera ? text1.match(/\n/g).length : 0, goForwardAll = is_opera ? (text1 + text2).match(/\n/g).length : 0;
if (selection.length == 0)
oTextHandle.setSelectionRange(newCursorPos + text1.length + goForward, newCursorPos + text1.length + goForward);
else
oTextHandle.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length + goForwardAll);
oTextHandle.focus();
}
oTextHandle.scrollTop = scrollPos;
}
// Can a text range be created?
else if ('caretPos' in oTextHandle && 'createTextRange' in oTextHandle)
{
var caretPos = oTextHandle.caretPos, temp_length = caretPos.text.length;

caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;

if (temp_length == 0)
{
caretPos.moveStart('character', -text2.length);
caretPos.moveEnd('character', -text2.length);
caretPos.select();
}
else
oTextHandle.focus(caretPos);
}
// Just put them on the end, then.
else
{
oTextHandle.value += text1 + text2;
oTextHandle.focus(oTextHandle.value.length - 1);
}
}


.. this will allow your smileys & BBCodes to work & fixes the errors you were getting.

Note: remember to clear the browser history/cache prior to testing anew.

My SMF Mods & Plug-Ins

WebDev

"Either you repeat the same conventional doctrines everybody is saying, or else you say something true, and it will sound like it's from Neptune." - Noam Chomsky

MobileCS

Any plans this will be officially addressed in a patch?

Advertisement: