IE 8 getSelection().anchorOffset alternative

余生长醉 提交于 2019-12-20 05:59:27

问题


I have got a Javascript code, which works well for modern browsers:

var offset = getSelection().anchorOffset;
var node   = getSelection().anchorNode;

How can I get the same result on IE 8, which does not have window.getSelection() method?


回答1:


There is a solution. It is possible to extend browser's object model using Rangy library:

window.getSelection = rangy.getSelection

As it may take some time for Rangy to init and as we will not need this for modern browsers, some more workaround:

/* 
IE 8 getSelection() missing object and properties simple hack
*/
if (window.getSelection == undefined) {                /* IE? */
    var wgS = setInterval(function(){                  /* wait for Rangy */
        if (rangy.initialized) {
            window.getSelection = rangy.getSelection;  /* do the stuff */
            clearTimeout(wgS);                         /* exit */
        }
     }, 10);
}


来源:https://stackoverflow.com/questions/18871180/ie-8-getselection-anchoroffset-alternative

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!