最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unable to get property 'createRange' of undefined or null reference - Stack Overflow

programmeradmin3浏览0评论

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"

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();

none of these seem to work ...???

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"

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();

none of these seem to work ...???

Share Improve this question edited Nov 3, 2013 at 20:01 Rob asked Nov 3, 2013 at 0:24 RobRob 3,8533 gold badges35 silver badges29 bronze badges 4
  • 1 Has it occurred to you that you might be trying to call createRange on a null or undefined reference? – Raymond Chen Commented Nov 3, 2013 at 0:53
  • yes, but that is not the problem - please see notes from awiebe below for more information – Rob Commented Nov 3, 2013 at 13:12
  • While it may not be the problem, you should have mentioned it as part of the "describe what you've tried" requirements of a question so that people don't waste their time exploring things you've already tried. – Raymond Chen Commented Nov 3, 2013 at 19:04
  • sorry about not having more detail Raymond, I've now added it above. The point I was trying to make was the one line of code that I originally had in place is no longer working - everything else around it seems to be ok. I get that there is a new DOM and that the old method is no longer supported - this was a great lead - but I'm still searching for a working equivalent to the original line code – Rob Commented Nov 3, 2013 at 20:05
Add a comment  | 

3 Answers 3

Reset to default 18

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.

Change document.selection.createRange().text to 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.

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.

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
发布评论

评论列表(0)

  1. 暂无评论