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 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
Related
I've got a script which runs on page load and does the following.
===== start of js file======
var curFldCtrl = Xrm.Page.getControl("transactioncurrencyid");
function ResetFieldLayout() {
curFldCtrl.setVisible(false);
}
function OnLoad() {
ResetFieldLayout();
}
===========end js file==============
Funny thing is that this code works find in Chrome and IE11, but when I run it in Microsoft Edge, it throws an error.
There was an error with this field's customized event.
Field:window
Event:onload
Error:Unable to get property 'setVisible' of undefined or null reference.
Anyone come across this before or know why this is happening?
Thanks in advance.
Try placing the variable declaration inside your function.
function ResetFieldLayout() {
var curFldCtrl = Xrm.Page.getControl("transactioncurrencyid");
curFldCtrl.setVisible(false);
}
function OnLoad() {
ResetFieldLayout();
}
Internet explorer is generating this error for the code that follows:
SCRIPT5007: Unable to get value of the property '0': object is null or undefined
for "document.getElementById('files').files[0]"
It's correct that ...files[0] is null, but I don't care, how can I tell IE not to care?
Thanks
var elem = document.getElementById('files'),
file = elem.files && elem.files[0];
This will short-circuit and return undefined if files is undefined, otherwise it will return the first file.
Point of clarification, the error indicates that files is the undefined variable and accessing the property 0 is causing an error. If it were files[0] itself, the expression would just return undefined.
Try...
var file;
if (document.getElementById('files').files){
file = document.getElementById('files').files[0];
}
This is very old but I want to answer for anyone who comes here like me
As Dennis says the error indicates that files is an undefined variable and I'm pretty sure that the problem is IE9 doesn't support files property.
As you can see here IE10 is requiered for.
So try this:
var file;
try{
file = document.getElementById('files').files[0];
}catch(error){
file= null;
}
PS: I apologize for my poor English
var file = document.getElementById('files').files[0] || "undefined";
I am trying to have a flash analog clock in my application and in its corresponding js i am trying to pass a value 'indiatime' which is system time.
document.getElementById('SBI_GLS_Analog_Latest').sendTextFromHtml(indiatime)
But this throws a script error in IE saying 'Object doesnt support this property or method'
Please help.
Thanks
I had a problem with getting Flash-Objects in Internet Explorer too.
It seems that you have to select them over the window-object rather than document in IE.
Try this little snippet, where flName is the id of the object:
function getFlashMovie(flName) {
var movieName = flName;
var isIE = navigator.appName.indexOf("Microsoft") != -1;
return (isIE) ? window[movieName] : document[movieName];
}
So now you can get the element:
var flashElement = getFlashMovie('flash_id');
I know that there are nicer ways to check for Internet Explorer, but this quick-fix did the trick for me.
So, I've created a function to do some error checking on an XML file that I recieve from an AJAX call. Part of the validation is that the function then builds out an object for easy access while I process that data into a form. In FF, works like a charm. IE dies with the error:
Object doesn't support this property or method
Here is the function (minus the un-important bits):
function checkReceiveXMLTagString( doc, messageObject, tag ) {
var tag_nodes = doc.getElementsByTagName( tag );
...do some error checking...
messageObject[tag] = tag_str; <-- error occurs on this line
return true;
}
Here is an example of how the function is call:
if ( checkReceiveXMLTagString( messageObject.Name[0], messageObject.Name[0], "First" ) ) {
...process messageObject.Name[0].First...
}
Like I said, FF has no issues. Safari loads the pages as well. IE has issues.
Thanks!
Looks like something is making messageObject be null or undefined
If the error is on this line:
messageObject[tag] = tag_str;
Then, the only two ways I know of that that can cause an error are:
messageObject is not an object that you can set properties on (null or undefined are the most likely ways that would happen)
tag is null or undefined
Since I see that your code calls this function hundreds of times so you can't effectively just break on it, I'd suggest you put in some defensive coding to check for those conditions and output something to the debug console to identify what the state is when the problem occurs. You can even trigger a conditional breakpoint with code like this:
if (!messageObject || !tag) {
debugger;
}
In the toughest cases, you can put an exception handler around it and break when the exception is thrown:
try {
messageObject[tag] = tag_str;
} catch(e) {
debugger;
}
Both of these will allow you to capture the condition in the debugger and examine all your parameters at the time of the error.
I have a fairly complex piece of Javascript that works flawlessly with no errors in Google Chrome, Firefox, Safari, and Opera. However, as tends to always be the endlessly annoying case, it completely fails in Internet Explorer. I have tested in IE7 and IE8 and get the same error:
Invalid argument. prototype.js, line
2216, character 9
I am using Prototype 1.6.1 hosted through Google. The error given isn't very helpful since it doesn't tell me where in my actual code the error is occurring. The line mentioned in the error is the 6th line from the bottom in the following code:
setStyle: function(element, styles) {
element = $(element);
var elementStyle = element.style, match;
if (Object.isString(styles)) {
element.style.cssText += ';' + styles;
return styles.include('opacity') ?
element.setOpacity(styles.match(/opacity:\s*(\d?\.?\d*)/)[1]) : element;
}
for (var property in styles)
if (property == 'opacity') element.setOpacity(styles[property]);
else
elementStyle[(property == 'float' || property == 'cssFloat') ?
(Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') :
property] = styles[property];
return element;
},
Since it is in the setStyle block of code, I assume the error occurs when I am setting style attributes for some element. However, I call setStyle over 100 times in this script and have been trying to figure out where exactly the error is occurring for several hours. Is there anything I can do to help myself in finding where the error is occurring?
Put an explicit try ... catch around the code:
for (var property in styles) {
try {
if (property == 'opacity') element.setOpacity(styles[property]);
else
elementStyle[(property == 'float' || property == 'cssFloat') ?
(Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') :
property] = styles[property];
}
catch (_) {
throw "Error setting property '" + property + "' to value '" + styles[property] + "'";
}
}
Then you'll know exactly what property and value is causing the problem.
In IE8 enable the developer tool to break on error [5th button on the script tab.] Click the Start Debugging button.
When the error occurs, you should be able to inspect the varaibles and see what is causing the problem exactly.
Try debugging with Microsoft® Visual Web Developer® 2010 Express, it's free and very easy to use while debugging on IE.
You've already marked your chosen answer so have probably found the cause by now. The line in question concerns setting opacity (which IE handles as it's proprietary filter) so I suggest looking for calls to setStyle that set an opacity, perhaps ones that set an unusual value.
The problem is caused by setStyle({someProperty: null}).
Maybe also undefined or something kind of.
To investigate such problems in future, inspect arguments you give to third-party functions in catch block. Kind of
try{
element.setStyle({someProperty: someValue})
}catch(error){
throw('' + someProperty + ':' + someValue)
}
This code would have pointed you to the source of the error in zero time. Here is more detailed snippet for debugging this case using some Prototype.js' helpers:
;(function () {
var superFunction = Element.setStyle
Element.addMethods({
setStyle: function (element, _arguments) {
try{
superFunction(element, _arguments)
}catch(error){
console.log(error, $H(_arguments).inspect())
}
}
})
})()
P.S. in IE8 you should open Developer Tools (F12) to have console working.