无法获取未定义或空引用的属性"createRange"

Rob*_*Rob 9 javascript

以下代码在我升级到Windows 8.1/Internet Explorer 11之前一直运行良好,现在抛出错误:"无法获取未定义或空引用的属性'createRange'"

var SelectedData = window.external.menuArguments.document.selection.createRange().text;
Run Code Online (Sandbox Code Playgroud)

有没有修复/解决这个问题?

*问题更新如下更新的代码仍然无效....

<html><head><title>-</title><script type="text/JScript">
function Launch()
{
var TheSelection = document.getSelection();
if(TheSelection != null)
{

.... do  a bunch of stuff

}
window.close();
}
</script></head><body onload="Launch();" </body></html>
Run Code Online (Sandbox Code Playgroud)

我也尝试过window.getselection; window.getselection(); .window.getselection()的ToString();

这些似乎都不起作用...... ???

Ray*_*hen 18

document.selection正确的文档在顶部说:

不再支持选择.从Internet Explorer 11开始,使用getSelection.有关信息,请参阅兼容性更改.

更改document.selection.createRange().textdocument.getSelection().

问题正是我所预测的.您正在调用createRange()null或undefined引用.具体来说,document.selection是未定义的.错误消息确切地说错了.

  • 你没有按照我的建议.您不仅将document.selection.createRange().text更改为document.getSelection().您还删除了window.external.menuArguments.把它放回去. (3认同)