Error on change form action - javascript

I'm changing the form action with:
window.onload = function() {
document.getElementById('pdf').onclick = addExportEvent;
document.getElementById('xls').onclick = addExportEvent;
document.getElementById('xml').onclick = addExportEvent;
document.getElementById('csv').onclick = addExportEvent;
}
function addExportEvent() {
data = grid.getAllGridData();
document.getElementById('dados').setAttribute('value', encodeURIComponent(JSON.stringify(data)));
formulario = document.getElementById('formulario'); // Line 55!
formulario.action = 'php/' + this.id + '.php';
formulario.submit();
return false;
}
But it doesn't work with Internet Explorer. It returns the following error:
Message: The object doesn't support the property or method.
Line: 55
Character: 2
Code: 0
URI: http://www.site.com/javascript/scripts.js

I think Andy E's head's comment hit it on the, well, head. You are assigning the correct element, but not declaring it using var which makes IE gag and choke and all kinds of bad stuff. Other browsers handle this just fine. So you are trying to access formulario instead of declaring it, which means it never gets the value of id: formulario
function addExportEvent() {
var data = grid.getAllGridData();
document.getElementById('dados').setAttribute('value', encodeURIComponent(JSON.stringify(data)));
var formulario = document.getElementById('formulario'); // Line 55!
formulario.action = 'php/' + this.id + '.php';
formulario.submit();
return false;
}

Related

In the function below, IE says that ')' is missing:

same code working in chrome and edge, but not working in ie
function PostAVWorkspaceTab(ParamURL, title = "") {
var DecodedURL = decodeURL(ParamURL);
const queryStringTitleValue = getQueryStringValueByKey(DecodedURL, 'Title');
var jsonData = {};
jsonData["MessageId"] = 1;
jsonData["Title"] = queryStringTitleValue ? queryStringTitleValue : title;
jsonData["URL"] = DecodedURL;
jsonData["ObjectId"] = 0;
try {
window.parent.postMessage(JSON.stringify(jsonData), "http://localhost:9002/TokenInfo");
}
catch (e) { }
console.log("PostAVWorkspaceTab(4): send message to open tab with URL = [" + DecodedURL + "] - " + "http://localhost:9002/TokenInfo" + jsonData);
}
Problem is in line PostAVWorkspaceTab(ParamURL, title = "") - to be specific the second parameter - title.
IE doesn't support default value in the parameter. Try this:
PostAVWorkspaceTab(ParamURL, title) {
if (title === undefined){
title = "";
}
....
}
As already informed by other community members, the IE browser do not support default parameters.
Reference:
Default parameters
This is the reason that you are getting an error in the IE browser.
To fix the issue you can try to remove the default value for the parameter and only try to pass the parameter will fix the issue.
You can try to implement any other logic like passing the empty value and check if the value is empty then try to use default value from the variable in function.

Cannot read property 'enumNodeFragments' of undefined

I'm trying to change the color of elements in 3D Viewer using the Autodesk-forge platform, and for this I'm using this API https://forge.autodesk.com/cloud_and_mobile/2015/12/change-color-of-elements-with-view-and-data-api.html by Daniel Du.
But the problem is when running I got this
The error Pict
And this the function :
Autodesk.Viewing.Viewer3D.prototype.setColorMaterial = function(objectIds, color) {
var material = addMaterial(color);
for (var i=0; i<objectIds.length; i++) {
var dbid = objectIds[i];
//from dbid to node, to fragid
viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, function () {
var it = viewer.model.getData().instanceTree;
console.log(it);
it.enumNodeFragments(dbid, function (fragId) {
var renderProxy = viewer.impl.getRenderProxy(viewer.model, fragId);
console.log("r prox : " + renderProxy);
renderProxy.meshProxy = new THREE.Mesh(renderProxy.geometry, renderProxy.material);
renderProxy.meshProxy.matrix.copy(renderProxy.matrixWorld);
renderProxy.meshProxy.matrixWorldNeedsUpdate = true;
renderProxy.meshProxy.matrixAutoUpdate = false;
renderProxy.meshProxy.frustumCulled = false;
viewer.impl.addOverlay(overlayName, renderProxy.meshProxy);
viewer.impl.invalidate(true);
}, false);
});
}
}
Hopefully, anyone has the solution to this problem...
Most likely you are running this code before the instance tree has been loaded, which provokes the error Cannot read property 'enumNodeFragments' of undefined on it variable. You would need to wait for the Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT before running that code.
Take also a look at previous question about modifying materials in the viewer.

Firefox not catching "undefined" error in javascript

I have some javascript code which calls a servlet with parameters based on the selected option from a SELECT list. It is possible that there will be no options in the selectCampaigns(account) so there is a try and catch.
The code in the catch should be called if,
var selectedCampaign = selectCampaigns.options[selectCampaigns.selectedIndex].text;
fails. This works in both chrome and IE but in firefox it gives a TypeErrror,
TypeError: selectCampaigns.options[selectCampaigns.selectedIndex] is undefined
which does not trigger the catch. May I have some suggestions on how to handle this? (in addition firefox does not show any of the alerts that I have inserted for debugging).
function selectcampaign(account) {
alert('alert1');
var selectCampaigns = document.getElementById("campaignSelect");
var urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart";
try {
var selectedCampaign = selectCampaigns.options[selectCampaigns.selectedIndex].text;
urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart?account=" + account + "&campaign=" + selectedCampaign;
} catch(err) {
alert(err);
urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart?account=" + account;
} finally {
window.location.href = urlstrg;
}
};
You should better test the value instead of dealing with errors.
function selectcampaign(account) {
var selectCampaigns = document.getElementById("campaignSelect");
var urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart?account=" + account;
if(selectCampaigns.options[selectCampaigns.selectedIndex]) {
urlstrg += "&campaign=" + selectCampaigns.options[selectCampaigns.selectedIndex].text;
}
window.location.href = urlstrg;
};

Removing whitespace in Javascript

Super newbie at Javascript here. I have a problem with whitespace that I'm hoping someone can help me with.
I have a function that looks like this:
function createLinks()
{
var i = 0;
tbody = document.getElementsByTagName('tbody')[3];
console.log('test order ID: '+document.getElementsByTagName('tbody')[3].getElementsByTagName('tr')[0].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,''))
trs = tbody.getElementsByTagName('tr');
console.log('trs.length = '+trs.length);
for (i=0;i<trs.length;i++)
{
orderId = trs[i].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,'');
console.log('order: '+orderId);
hrefReturn = 'https://www.example.com/example.html?view=search&range=all&orderId='+orderId+'+&x=13&y=17';
linkReturn = '<a href='+hrefReturn+'>'+orderId+'</a>';
console.log(linkReturn);
trs[i].getElementsByTagName('td')[1].innerHTML = linkReturn;
}
}
I call this function using another function when the page is initially loaded. This works perfectly.
However, I also call this function in another way when data on the page changes. There's a dropdown list that I have an onClick attribute attached to. That onClick event calls the first function, which in turn calls the second function (above). Both of these functions are saved into text variables and appended to the document, as below:
var scriptTextLinks = " function createLinksText() { var i = 0; tbody = document.getElementsByTagName('tbody')[3]; console.log('test order ID: '+document.getElementsByTagName('tbody')[3].getElementsByTagName('tr')[0].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,'')); trs = tbody.getElementsByTagName('tr'); console.log('trs.length = '+trs.length); for (i=0;i<trs.length;i++) { orderId = trs[i].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,'').replace(/\s/g,''); orderId = orderId.replace(/\s/g,''); console.log('order: '+orderId); hrefReturn = 'https://www.example.com/example.html?view=search&range=all&orderId='+orderId+'+&x=13&y=17'; linkReturn = '<a href='+hrefReturn+'>'+orderId+'</a>'; console.log(linkReturn); trs[i].getElementsByTagName('td')[1].innerHTML = linkReturn; } console.log('complete'); } "
Finally, here is the specific problem. When THIS version of the same function is called by events on the webpage, it fails to properly delete the whitespace, which breaks the link that it's supposed to create.
This is the exact problem section of code:
orderId = trs[i].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,'').replace(/\s/g,''); orderId = orderId.replace(/\s/g,''); console.log('order: '+orderId);
So instead of storing the variable like it should, like this:
"XXXXXXXXX"
It is stored like this:
"
XXXXXXXXXX
"
Which, again, kills the link.
Can anybody clarify what's going on here, and how I can fix it? Thanks in advance!
To strip all that surrounding whitespace you can use the standard .trim() method.
var myString = " \n XXXXXXX \n ";
myString = myString.trim();
You can strip all leading and trailing, and compress internal whitespace to a single space, as is normally done in HTML rendering, like this...
var myString = " \n XXXX \n YYYY \n ";
myString = myString.replace(/\s+/g, " ").trim();
Also, see tharrison's comment below.
(though my /\s+/g pattern worked fine with the embedded \n newlines)
Cure for IE<9
"shim.js"
(function() {
if (! String.prototype.trim) {
//alert("No 'trim()' method. Adding it.");
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/mg, '');
};
}
})();
(Or, if you might want to do other things in your shim...)
var shim = {
init: function() {
if (! String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/mg, '');
};
}
}
};
shim.init();
Your HTML file
<script type="text/javascript" src="shim.js"></script>

Javascript error while using EncryptedLocalStore

I am trying to make my first JS-based adobe air app.
But I've stuck at a point.
Here's the code which is causing the error
var RunUrl = 'http://www.lilpirate.net';
var firstRunUrl = 'http://www.netbloo.com';
var snxApp = air.EncryptedLocalStore.getItem( 'snxApp' );
var semail = snxApp.readUTFBytes( snxApp.bytesAvailable );
if( semail!='786') {
data = new air.ByteArray();
data.writeUTFBytes( '786' );
air.EncryptedLocalStore.setItem( 'snxApp', data );
var snxUrlToLoad = firstRunUrl;
}
else
var snxUrlToLoad = RunUrl;
When compiling it from adl, it throws error -
TypeError: Result of expression 'snxApp' [null] is not an object.
Help!
You are accessing properties (bytesAvailable and readUTFBytes) of snxApp without checking to make sure it exists first. If you haven't used setItem to store anything with that name yet, it will be null.
Here's an example of how it would look with an if statement:
var snxApp = ...;
var semail;
if (snxApp !== null) {
semail = snxApp.readUTFBytes( snxApp.bytesAvailable );
}
...

Categories

Resources