Problem using window.location in a function called from onsubmit - javascript

I'm currently creating a very small form on my homepage using HTML and JavaScript. I've run into a problem I'm pretty sure I could circumvent somehow (probably in a pretty ugly way though), but I got interested in why this error appears.
I have a form on my page which I specify in the following way:
<FORM name="form1" onsubmit="submitTheScript()">
The function "submitTheScript()" is placed in the header and reads:
setCookie("F_GivenSum", GivenSum);
window.location="LastScreen.html";
"setCookie()" is a function that basically just, well, creates a cookie :).
Now, the problem arises with the last line of code. I'm trying to send the user to another page, after the cookie has been set (I'm doing some controls that the input value is alright, but I've skipped that part here) but FireFox gives me the following error:
uncaught exception: [Exception... "Cannot modify properties of a WrappedNative" nsresult: "0x80570034 (NS_ERROR_XPC_CANT_MODIFY_PROP_ON_WN)" location: "JS frame :: chrome://global/content/bindings/autocomplete.xml :: onxblpopuphiding :: line 827" data: no]
I guess I can't call this function the way I do, from within a onsubmit command, however, I don't see why.

Okay, I did a quick test and found out that you need to assign the onsubmit event handler via javascript like so:
document.getElementById("myform").onsubmit = doSubmit;
function doSubmit() {
document.cookie = "F_GivenSum=200";
window.setTimeout(function() {
window.location = ('test2.html');
}, 20);
return false;
}
This did not give any errors in FF.

Related

HtmlUnit JavaScript Issue on page load - Cannot find function

I'm crawling website secured with Cloudflare and sometimes getting an error due to redirection to page with ReCapcha, the page cannot be even loaded due to some javascript error. The code is failing on #getPage method and i have no idea why.
Here is the code works fine for normal pages, but fails on confirmation page:
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setJavaScriptEnabled(true);
final HtmlPage page = webClient.getPage("https://mydummy.site");
webClient.waitForBackgroundJavaScript(10000);
int waitForBackgroundJavaScript = webClient.waitForBackgroundJavaScript(200);
int loopCount = 0;
while (waitForBackgroundJavaScript > 0 && loopCount < 2) {
++loopCount;
waitForBackgroundJavaScript = webClient.waitForBackgroundJavaScript(200);
if (waitForBackgroundJavaScript == 0) {
break;
}
}
Logs:
java.lang.RuntimeException: com.gargoylesoftware.htmlunit.ScriptException: Wrapped com.gargoylesoftware.htmlunit.ScriptException: Wrapped com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot find function start in object [object MessagePort]. (https://www.gstatic.com/recaptcha/api2/v1536705955372/recaptcha__en.js#249) (https://www.gstatic.com/recaptcha/api2/v1536705955372/recaptcha__en.js#253)
at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:305)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:539)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:399)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:316)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:467)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:449)
at Main.htmlUnit(Main.java:156)
at Main.main(Main.java:43)
Caused by: com.gargoylesoftware.htmlunit.ScriptException: Wrapped com.gargoylesoftware.htmlunit.ScriptException: Wrapped com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot find function start in object [object MessagePort]. (https://www.gstatic.com/recaptcha/api2/v1536705955372/recaptcha__en.js#249) (https://www.gstatic.com/recaptcha/api2/v1536705955372/recaptcha__en.js#253)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:892)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:616)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:532)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:772)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:748)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:104)
at com.gargoylesoftware.htmlunit.html.HtmlPage.loadExternalJavaScriptFile(HtmlPage.java:992)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:371)
at com.gargoylesoftware.htmlunit.html.HtmlScript$2.execute(HtmlScript.java:246)
at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:298)
We have been struggling with this issue as well. Our test suite ran perfectly until late 2018 when this issue broke all of our logins. I believe Google has put this in deliberately to break automated attempts to break captchas, because solving one part of this seems to only lead to another problem. Both loading the page and submitting the page causes issues, even if you tell HtmlUnitDriver to ignore all JavaScript errors.
I have tried several options at this point. If you use the Google specified test site key, then the errors go away. So if you have full server-side control of how that site key is generated, you are OK. Remember to ensure that the test site key shows up again on validation errors and all similar use cases, otherwise you will get that error.
(Unfortunately for us, our login page is plain JSP and so implementing this is a headache unless we want to change the URL everywhere. Still debating what to do, for right now we do have a workable if ugly solution that involves some conditional logic on the page and catching JavaScript exceptions at other points in the test code.)

In Netsuite, ReferenceError nlapiCreateFile and nlapicreatecsvimport is not defined

In netsuite, I write the these code but I face a problem as
ReferenceError nlapiCreateFile is not defined
Code:
function creatingFile() {
var file = "xxxxx";
var fileObj = nlapiCreateFile('mycsv.csv','CSV',file); // Error !!!
nlapiSendEmail(15,'epost#xx.com,"subject" ,fileObj, null, null, null, null);
}
I have some similar old 1.0 code that works just fine. From a quick glance that is probably failing due to the "file" variable not being defined, and you have an extra apostrophe in the nlapiSendEmail function. Is that defined as a global variable earlier on in your script, also is "x" defined as an email address or internal ID?
Try running it with some try catch statements to get more specific error information. This should give you enough information for debugging where your problem is at.
function creatingFile(){
var fileContents='ASDFASDf,asdfgasfasdf';
var folderID='12345'; //replace with a valid folder internal ID.
try{
var fileObj=nlapiCreateFile('mycsv.csv','CSV',fileContents);
fileObj.setFolder(folderID);
var fID=nlapiSubmitFile(fileObj);
}catch(err01){
nlapiLogExecution('DEBUG','ERROR 01',JSON.stringify(err01));
}
try{
nlapiSendEmail(15,x,"asd",fID,null,null,null,null);
}catch(err02){
nlapiLogExecution('DEBUG','ERROR 02',JSON.stringify(err02));
}
}
I believe you are running into what I would consider a "bug", but NetSuite may disagree. If you are calling your creatingFile() function from a user event (i.e. a button on the Suitelet form, etc), that seems to fall under the category of "client script" in NetSuite logic, and the nlapiCreateFile function is not available in Client script context. This is not at all intuitive because, from my perspective, it's all part of the Suitelet, which is supported for that function. The only way I found to work around it is to create the file on load of the script, not on action of the user. However, this has resulted in other issues I haven't figured out yet, such as how I use the newly created file object in the client context once the customer clicks the button (I'm trying to send the file as an email attachment).

Infragistics ig.WebCapture Error

$('#btnTest').click(function () {
var captcha = $IG.WebCaptcha.find('capWebCaptcha');
alert(captcha.get_validationFailed());
// alert($IG.WebCaptcha);
});
This is throwing the following error:
Uncaught TypeError: $IG.WebCaptcha.find is not a function
I found the example from the URL below:
http://www.infragistics.com/help/aspnet/webcaptcha_captcha_validation.html
I am following all the instructions as provided. Has anybody seen this before or can somebody offer a work-around?
You can try to get reference using var captcha = ig_controls.capWebCaptcha, where the Id may differ if teh control is placed in panel, loaded from user control, etc.

Setting break point changes behavior of code

I'm attempting to implement the jquery.FileDownload plugin that allows for an Ajax-like file download experience that isn't normally possible by using iframes. The fact that I'm using this plugin isn't imperative to my question, however. This is a general question about break points in code.
That being the case, here is my code:
function generateReport(elementId, data, url) {
var $container = $(elementId);
$.fileDownload(url, {
httpMethod: 'POST',
data: data,
successCallback: function (url) {
$container.toggle('slow');
},
failCallback: function (responseHtml, url) {
$container.find('.error-message').removeClass('hide');
}
});
return false;
}
When the AJAX call is successful, successCallback is called and everything works properly. When a failure occurs in the request, failCallback is called and I am unable to access any elements in $container. I get a "Permission denied" error which may indicate something is going on regarding accessibility because of the iframe.
In either case, when debugging this issue, I put a break point in my browser debugger on the $.fileDownload() line. When I break on this line for any amount of time, $container and all of its containing elements are accessible and I don't get the "Permission denied" error. This is also the case when I break for any amount of time in the server-side method corresponding to url that is being passed into the $.fileDownload method.
This made me think there was a timing/threading issue with this plugin in IE. To recreate the delay that prevented the error from occurring in my debuggers, I tried to use setTimeout() and Thread.Sleep() in the javascript and server-side code respectively, but this still resulted in the "Permission denied" error.
What is different from breaking in the debuggers compared to manually adding a delay in the code?

Firebug not showing Javascript errors for Rails applications

I have a Rails application, and when I have Javascript errors they are not showing in the Firebug console. I have 'Show javascript errors' and 'Show javascript warnings' selected.
When I insert javascript errors in a basic html file, the errors show as expected.
In the javascript of the Rails app, it only shows errors in rare cases.
For example i can insert nonsense like:
dfghaefb;
and no error is shown in Firebug. But if i insert a space in there Firebug does show the error:
dfgh aefb;
Any ideas? This is driving me nuts.
UPDATE:
Pumbaa80 was right, it's syntax vs runtime.
So I set up onerror:
onerror=errorHandler;
var error="";
function errorHandler(errMessage,url,line){
error="There is an error at this page.\n";
error+="Error: " + errMessage+ "\n";
error+="URL: " + url + "\n";
error+="Line: " + line + "\n\n";
error+="Click OK to continue viewing this page,\n";
alert(error);
return true;
}
And I have a method with an error:
function initForm() {
asdfs;
}
And it works when I call it outside of a method:
initForm();
but not in this case:
document.observe('dom:loaded', function() {
initForm();
});
Why is that?
dfghaefb;
produces a run-time error. Those errors may be suppressed by putting an onerror handler on the window or by trapping them in a try/catch in some way. In that case, Firebug won't show anything.
In contrast,
dfgh aefb;
is a syntax error, which is shown in the error console, regardless of try/catch and onerror.
Did you let the page load completely? I have a rails app running in front of me right now with firebug on and i entered dfghaefb; and yes it does throw an error!
ReferenceError: dfghaefb is not defined { message="dfghaefb is not defined", more...}
Running Firefox 3.6.3, Firebug 1.5.3 and MacOSX 10.5.6 :)
This may appear to be obvious, but be sure you wrap your inline JS using
<script type="text/javascript"> code </script>
Otherwise FireBug won't look at it at all.
I had the same, it wasn't showing anything and it was driving me crazy for ages, even if i had "Show javascript errors" in the console tab selected, it seems that it started working after I did "Enable all panels".
These kinds of problems cannot be answered without a test case.

Categories

Resources