javascript 无法获取未定义或空引用的属性“createRange”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19748500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to get property 'createRange' of undefined or null reference
提问by Rob
The following code, which worked well right up until I upgraded to windows 8.1 / Internet Explorer 11, is now throwing an error: "Unable to get property 'createRange' of undefined or null reference"
以下代码在我升级到 Windows 8.1/Internet Explorer 11 之前一直运行良好,现在抛出错误:“无法获取未定义或空引用的属性‘createRange’”
var SelectedData = window.external.menuArguments.document.selection.createRange().text;
Is there a fix / work around for this?
是否有修复/解决方法?
*Question updated below with newer code that is still not working ....
*问题在下面更新,更新的代码仍然无法正常工作....
<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>
I have also tried window.getselection; window.getselection(); window.getselection().tostring();
我也试过 window.getselection; window.getselection(); window.getselection().tostring();
none of these seem to work ...???
这些似乎都不起作用......?
回答by Raymond Chen
The documentation for document.selection
says right at the top:
selection is no longer supported. Starting with Internet Explorer 11, use getSelection. For info, see Compatibility changes.
不再支持选择。从 Internet Explorer 11 开始,使用 getSelection。有关信息,请参阅兼容性更改。
Change document.selection.createRange().text
to document.getSelection()
.
更改document.selection.createRange().text
为document.getSelection()
。
The problem was exactly what I predicted. You are calling createRange()
on a null or undefined reference. Specifically, document.selection
is undefined. The error message said exactly what was wrong.
问题正是我所预测的。您正在调用createRange()
null 或未定义的引用。具体来说,document.selection
是未定义的。错误消息准确地说明了错误所在。
回答by awiebe
That's really not very much context, but generically, your error message means you have failed to do this:
这真的不是很多上下文,但一般来说,您的错误消息意味着您未能做到这一点:
var SelectedData;
var selection = window.external.menuArguments.document.selection;
if(selection != null)
{
SelectedData = selection.createRange().text;
}
When you tried to get the selection, no selection had been made, hence the selection was null. When an object is null you cannot query it, because the structure containing the information you need does not exist.
当您尝试获取选择时,没有进行选择,因此选择为空。当对象为空时,您无法查询它,因为包含您需要的信息的结构不存在。
回答by Santos L. Victor
For this adjustment, you can find:
对于此调整,您可以找到:
b=document.selection.getSelection()
Or something like it Then you can chance it using this code below:
或类似的东西然后您可以使用以下代码进行操作:
b=typeof document.selection!=="undefined"?document.selection.getSelection():null