Workaround for IE's document.all.table.rows() 'function' - javascript

I have a script on a website that has been coded for IE. I can't edit the original script, so i'm trying to alter it through js and Jquery (by using a Chrome Plugin).
The script uses next line to add a row to an HTML-table, but gives errors in Chrome:
document.all.tbl.rows(i);
after some research I found out this is a function only used in IE. In Chrome an array is used.
So all these lines should be altered to the following:
document.all.tbl.rows[i]
The same problem occurs with rows[i].cells().
I've tried moving the array to rowsData and overwriting rows to a function retrieving the data from rowsData.
document.all.tbl.rowsData = document.all.tbl.rows; // This seems to work
document.all.tbl.rows = function(id) {
console.log('This function is used');
}
After checking with Chrome's devtools, I've found that rowsData does contain a copy of rows, but rows is still an array. When using document.all.tbl.rows() I still get the same error:
Uncaught TypeError: document.all.tbl.rows is not a function
The only other option is replacing the entire function (which is quite long, and not preferred because there's always a probability it can change).
I'm guessing that overriding a browser variable isn't possible.
Is there some way I can make this work?

Related

Object property in Javascript exists in the larger object but is null when accessing just that property

I'm new to JavaScript (and most coding concepts in general) and this feels like there must be some simple solution that I just haven't found yet. I need information from a property of an object (not totally sure if I'm using those terms correctly). The object is created using ArcGIS API for Javascript.
console.log(view.popup);
console.log(view.popup.id);
console.log(view.popup.title);
By logging to the console (using the lines of code shown above) I can see that the properties exist because the below lines are logged to the console (from Line 1).
id: "17e2bf83e50-widget-1"
title: "k0"
I then log just the id property (Line 2) and it prints just the id property. However, if I try to log view.popup.title (Line 3), it logs 'null' to the console. Anything else I try to print out using console.log prints the same value found within view.popup. I just need to be able to use that value stored within view.popup.title and for some reason it seems like the only one where I can see that it's there but can't access it directly?
Edit: This does certainly seem to be the issue commented on by folks below, thanks for those links! I've been trying to do stringify (as suggested in Is Chrome’s JavaScript console lazy about evaluating objects?) but finding that it only finds some of the properties. I also made an attempt at making it wait to try to find it until the property is no longer null, but it's definitely somehow later in the code where that happens (the code just enters an infinite loop until there's a call error).
View is generated using https://developers.arcgis.com/javascript/latest/api-reference/esri-views-SceneView.html#popup (which is why I don't have a full understanding of what goes into it/when and how it makes that view.popup.title property)
view = new SceneView({
viewingMode: "local",
map: scene,
container,
camera,
spatialReference: {
wkid:3089
}
});
After learning here that it's related to view.popup.title not existing yet at the point of logging it, I moved over to the ArcGIS forum to ask about how view is generated and I got an answer that lets me access it! https://community.esri.com/t5/arcgis-api-for-javascript-questions/accessing-title-of-default-popup-created-for-a/m-p/1131210#M75813
view.popup.watch("title", () => {
console.log(view.popup.title);
});

Getting “This operation is not supported from a callback function” error when transposing array

First apologies for this question but GAS is a new syntax to me.
I would like to do something that is simple in VBA syntax, but not in GAS.
I want to transpose one named range to another named range (e.g. from 4 rows to 4 columns) on different worksheets. There are examples on the web that use .getDataRange() but I wish to use a fixed named range, whether there is data contained within or not.
The syntax I am playing around with is the following:
var wksInput = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('New');
var wksDB = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Database');
var intDBNewRow = wksDB.getLastRow() + 1;
wksDB.getRange(intDBNewRow, wksDB.getRange('dbContacts').getColumn(), 1,wksDB.getRange('dbContacts').getNumColumns()).setValues(transposeRange(wksInput.getRange('frmVanContacts').getValues()));
Taking 4 rows from a fixed point on an input form worksheet (wksInput) to 4 columns on the last row of a recordset/database worksheet (wksDB)
Where transposeRange is:
function transposeRange(a)
{
return Object.keys(a[0]).map(function (c) { return a.map(function (r) { return r[c]; }); });
}
The error is:
This operation is not supported from a callback function.
What is wrong here?
Answer
The problem isn't with the transpose function, the problem is that the Google Apps Script debugger can't do step into callbacks by design, so when debugging your code, instead of doing a debug step into, put a breakpoint before and after the call of the transpose function in order to make the debugger to skip that call.
References
In Google Apps Script Issues there is a issue about this -> Issue 4662: Debug breakpoint within the function body of an Array.map() call causes a server error that has the following comment from a Googler
From https://code.google.com/p/google-apps-script-issues/issues/detail?id=4662#c3
This is currently working as intended -- we do not support debugger
breakpoints withing callback functions. This issue will therefore be
marked as a Feature request.
A related question with no answers yet but with informative comments that help me to find the above reference
Debugger fails with "This operation is not supported from a callback function."

What is the browser neutral replacement for HTMLFormElement's all method?

I'm using an institutional (ie, any fix will have to be on my side) website that is IE specific, but I want to use it with Safari. The website mostly works, but at one point I get the following error in my console:
Uncaught TypeError: Object #<HTMLFormElement> has no method 'all'
When I dig into the Javascript the error is coming from:
function fnFocus() {
var frmCtl = document.frmAddEditAdultPosition ;
if(frmCtl !=null) {
var ctlFN = frmCtl.all("txtFirstName") ;
ctlFN.focus() ;
}
}
Calls to the all method are scattered throughout the code.
My plan, is to use proxypy to fix the Javascript as it comes in. I'm assuming that the all method is something IE specific, but I don't know what I should replace it with.
The elements collection:
frmCtl.elements["txtFirstName"] // Might be another collection if there are duplicate fields of that name
or getElementsByName:
frmCtl.getElementsByName("txtFirstName") // Always a collection
or querySelector:
frmCtl.querySelector('[name="txtFirstName"]') // Gets the first match
or querySelectorAll:
frmCtl.querySelectorAll('[name="txtFirstName"]') // Always a collection

"SCRIPT438: Object doesn't support property or method 'url' " in IE only

I am getting the following error in IE (but not Firefox):
SCRIPT438: Object doesn't support property or method 'url'
AjaxSetup.js?version=7b8dcb65-17d1-437f-9594-0621c779427c, line 28 character 2
There are several other posts with errors like this (for other objects besides url), but all of them seem to have answers along the lines of "such a function doesn't exist in jquery" or "such a function is invalid to use in this context", and neither seems to apply to my situation at least as far as I can tell.
The function containing the line number that the error is referring to is:
function redirectToLogin() {
var redirUrl = $.url().attr("path");
if ($.url().attr("query").length > 0) {
redirUrl += "?" + $.url().attr("query");
}
top.window.location = "/Shared/Logout?redir=" + encodeURIComponent(redirUrl);
return;
}
where line 28 is the second line of the function above.
More strangely, while on the offending page (from which the above function gets called), when I type $.url() or $.url().attr("path") into the IE Developer Tools console, it returns the correct object and string, respectively. The values also seem to stay correct if I "watch" them.
Any help would be much appreciated!
EDIT:
I found a workaround:
function redirectToLogin() {
top.window.location = "/Shared/Logout?redir=" + encodeURIComponent(location.pathname + location.search);
return;
}
This seems to work and achieve the same thing, so I'm posting it in case it helps someone. However, I would still be curious to find out why the original code using jquery was not working.
I would guess if you say it works in the console that the code is trying to use it before $.url() is initialized. Is the url JavaScript included before the AjaxSetup file?

IE Object doesn't support this property or method

This is probably the beginning of many questions to come.
I have finished building my site and I was using Firefox to view and test the site. I am now IE fixing and am stuck at the first JavaScript error which only IE seems to be throwing a hissy about.
I run the IE 8 JavaScript debugger and get this:
Object doesn't support this property or method app.js, line 1 character 1
Source of app.js (first 5 lines):
var menu = {};
menu.current = "";
menu.first = true;
menu.titleBase = "";
menu.init = function(){...
I have tested the site in a Webkit browser and it works fine in that.
What can I do to fix this? The site is pretty jQuery intensive so i have given up any hope for getting it to work in IE6 but I would appreciate it working in all the others.
UPDATE: I have upload the latest version of my site to http://www.frankychanyau.com
In IE8, your code is causing jQuery to fail on this line
$("title").text(title);
in the menu.updateTitle() function. Doing a bit of research (i.e. searching with Google), it seems that you might have to use document.title with IE.
Your issue is (probably) here:
menu.updateTitle = function(hash){
var title = menu.titleBase + ": " + $(hash).data("title");
$("title").text(title); // IE fails on setting title property
};
I can't be bothered to track down why jQuery's text() method fails here, but it does. In any case, it's much simpler to not use it. There is a title property of the document object that is the content of the document's title element. It isn't marked readonly, so to set its value you can simply assign a new one:
document.title = title;
and IE is happy with that.
It is a good idea to directly access DOM properties wherever possible and not use jQuery's equivalent methods. Property access is less troublesome and (very much) faster, usually with less code.
Well, your line 1 certainly looks straight forward enough. Assuming the error line and number is not erroneous, it makes me think there is a hidden character in the first spot of your js file that is throwing IE for a fit. Try opening the file in another text editor that may support display of normally hidden characters. Sometimes copying/pasting the source into a super-basic text-editor, like Notepad, can sometimes strip out non-displayable characters and then save it back into place directly from Notepad.

Categories

Resources