How do I handle an error in code below? Let’s say that elements don’t exist in the webform?
Dim jsSrc As String
jsSrc = "document.forms[0].elements['search'].value=""xojo"";"
HTMLViewer1.ExecuteJavaScript(jsSrc)
How do I handle an error in code below? Let’s say that elements don’t exist in the webform?
Dim jsSrc As String
jsSrc = "document.forms[0].elements['search'].value=""xojo"";"
HTMLViewer1.ExecuteJavaScript(jsSrc)
var el = document.forms[0].elements['search'];
If(el) {
El.value = "Xojo";
}
You can put all of that into the string that you send to the htmlviewer.
Hi. That doesn’t work. Basically I’m trying to handle if the elements[‘search’] field doesn’t exist, so I don’t execute the script which will lead to an application crash; An exeption of Htmlviewer Class not handled!
JavaScript errors shouldn’t cause HTMLViewer to crash, they just kind of silently fail.
If I try to post to an element that doesn’t exist the form the app crashes.
The code Greg posted prevents just that.
Sorry I don’t know how to get the java script to work in the example. Can anyone combine these line together in a workable code
If I can get this right I can figure out the rest.
How and where will this be added:
var el = document.forms[0].elements[‘search’];
If(el) {
El.value = “Xojo”;
}
To this: to handle error:
Dim jsSrc As String
jsSrc = “document.forms[0].elements[‘search’].value=”“xojo”";"
HTMLViewer1.ExecuteJavaScript(jsSrc)
var el = document.forms[0].elements['search'];
If(el) {
El.value = "Xojo";
}
That is the whole JavaScript that needs to be executed.
So,
dim jsSrc as string = "var el = document.forms[0].elements['search']; if(el){el.value = ""Xojo"";}"
HTMLViewerThatNeedsAUniqueName.ExecuteJavaScript(jsSrc)
Tim, thanks a lot. I got the hang of it now… My first time trying to add Javascript to Xojo.
An elegant way of using well formatted Javascript in Xojo is to paste it into a constant with all the endOfLines, and then remove them for use. For instance, paste Greg’s code in a constant named searchValue,
Constant searchValue :
var el = document.forms[0].elements['search'];
If(el) {
El.value = "Xojo";
}
and for use go
HTMLViewer1.ExecuteJavaScript(replaceAll(searchValue, endOfLine, ""))
That way you keep your Xojo code compact, while retaining the legibility of JavaScript.
Can someone help me figure out what is wrong with this code:
dim jsSrc as string = "var el = document.forms[0].elements['WakeUp']; if(el){el.click;}"
Me.ExecuteJavaScript(jsSrc)
Here is the code for the form (Please note that I cannot change the form, I just need to be able to click the Wakeup button if it exists):
[code] <?xml version="1.0" encoding="UTF-8"?>
10.0.0.178
![]() | HP PageWide XL 8000PS | ![]() |
|
Does the receiver check the value of WakeUp
? If not you can just submit the form.
Me.ExecuteJavaScript("document.forms['WakeUp'].submit();")
If it does check to see the value of the submit button was sent, you could just add another hidden field that mimics the button value. Not as simple to do, but should work. (Yes, it can be done with JavaScript, without modifying the page)
A far more elegant way to handle this would be to submit your own POST request with a HTTPSocket.
I have a big project which uses HTMLViewers extensively, and I spent additional effort to make a version of the pure HTML / JavaScript code which can run in a normal browser (Safari, and IE 11). Both of these have really good built-in javascript debuggers, and although it took a little more time to set this up, it has paid off. I can now pretty much test & debug my javascript outside of Xojo, only testing it in Xojo once it’s well debugged.
If that’s too hard, some easier tricks may work - try wrapping your code in a try() block which outputs errors to window.title and then put logging in the HTMLViewer.TitleChanged event
Sub Utils.DoJavascript(extends htm as HTMLViewer, cmd as String)
dim s as string
dim eol as string = " "
s = s + "try " + eol
s = s + "{" + eol
s = s + " document.title = " + cmd + ";" + eol
s = s + "}" + eol
s = s + "catch(err)" + eol
s = s + "{" + eol
s = s + " document.title = 'error:' + err.description ; " + eol
s = s + "}" + eol
htm.executeJavaScript(s)
Sorry, I should have been clearer that when someone visits the site, there may or may not be a Wakeup button, so it does need to check if it exists or not.
You should get the form submit working first, then worry about the part that decides not to submit the form.
Tim, sorry I haven’t fully explained what I am trying to accomplish here and that’s my bad.
We have a large format HP plotter that goes to sleep periodically, but in order to do some routine maintenance, I need to wake it up periodically.
In order to wake it up, I have to click the Wakeup button on a form on it’s web utility. If the plotter is already awake, the form does not exist, it instead takes us into the web utility. So I need to figure out a way to click the button if it is there, but to do nothing if it isn’t there.
This should work :
var el = document.getElementsByClassName('buttonA')[0]; if(el != undefined){el.click;}"
[quote=250432:@Michel Bujardet]This should work :
var el = document.getElementsByClassName('buttonA')[0]; if(el != undefined){el.click;}"
This still results in an error for some reason, but I do agree that it should still work.
[quote=250331:@Tim Parnell]Does the receiver check the value of WakeUp
? If not you can just submit the form.
Me.ExecuteJavaScript("document.forms['WakeUp'].submit();")
If it does check to see the value of the submit button was sent, you could just add another hidden field that mimics the button value. Not as simple to do, but should work. (Yes, it can be done with JavaScript, without modifying the page)
A far more elegant way to handle this would be to submit your own POST request with a HTTPSocket.[/quote]
This fails as well and I tried the HTTPSocket post, but that doesn’t seem to be working either.
It would greatly help if you gave more details on how it fails, possibly the JavaScript error if any. Without any debugging tools, nobody can assist with no information.
Are you sure of the page code ?