JavaScript Execution on HTMLViewer/ Elements

Hi all!

I discovered that I can execute Javascript in HTMLViewer. for example to search data, reset textfields on a webpage.

But I have this code to get the number of “Form” elements presents on the Source code, thats what I’m using:

Dim jsSrc As String

jsSrc = “document.forms.length;” //Get the number of Form element is found
HTMLViewer1.ExecuteJavaScript(jsSrc)

But how can I get the returned value to pass it on a Msgbox.?

Regards

Mac or Cross Platform?

Cross platform, or Windows only you’ll have to use the window.status trick.

HTMLViewer.ExecuteJavaScript("window.status = document.forms.length;")

and then use the StatusChanged event to capture the new status and set it to a variable.

If you’re using just Mac there’s a much more reliable declare.

Thanks I gonna probe it. Its Cross platform

One more question If I need to find all the Image values on the Source code, ex.

How can I get a list of the Links of all Images sources?
I thought this, but appears not to be correct:

HTMLViewer1.ExecuteJavaScript(“window.status= document.getElementById(”“img”").elements[0].value")

[quote=231483:@Gerardo García]One more question If I need to find all the Image values on the Source code, ex.

How can I get a list of the Links of all Images sources?
I thought this, but appears not to be correct:

HTMLViewer1.ExecuteJavaScript(“window.status= document.getElementById(”“img”").elements[0].value")[/quote]

HTMLViewer1.ExecuteJavaScript("imgElementsArray= document.getElementsByTagName(""img"")[0]")

http://www.w3schools.com/jsref/met_document_getelementsbytagname.asp

Then loop through all the items in the array with a for next
http://www.w3schools.com/js/js_loop_for.asp

Use the Length property to find the number of items :
http://www.w3schools.com/js/js_arrays.asp

[quote=231494:@Michel Bujardet]HTMLViewer1.ExecuteJavaScript("imgElementsArray= document.getElementsByTagName(""img"")[0]")

http://www.w3schools.com/jsref/met_document_getelementsbytagname.asp

Then loop through all the items in the array with a for next
http://www.w3schools.com/js/js_loop_for.asp

Use the Length property to find the number of items :
http://www.w3schools.com/js/js_arrays.asp[/quote]
Thanks for post Michel. Well thats what I wanna do, I want to get the attribute “onclick” of the IMG objects, like the following code:

<img id=“BtnDescarga” name=“BtnDescarga” src=“Images/World-download.png” height=“25” width=“25” class=“BtnDescarga” onclick="return AccionCfdi(‘RecuperaCfdi.aspx?Datos=hNDX+PXkv+OISfvZ8Ty8ZCIamIZSmnCcV4mvnvLFoH9oR9CpdR3HooNSGW2+f6xA5sw8AxTqJU6Zv8eVjgxo0V0RH0cZSXeMokaQeP9AFS4kig1oY4z78jqYAxvDQiFaVCHJsU1ORg/SYc2EObQ3pEjQBbS7MKQkdfi0dRfJL1vG+dfuH+UvRffktruGSIER7ST7LuTdYjC+olsNPfb7TFk6NR1VOEOkVPQ3Uk5J4yJGioTTKtSLYP90z/nA8WRmF/mM1sYQ0eMTgpnEK1W0b5fenLcddyOqvwt244KhS+N0mJILhgNtMQJWLSPrhJrSZJjKpnC5MxXNrr0FfuFMwvTBd7AuJqIuEJ6npcmTwOPOYIfUtnxw42CMrXjjTNy1FFWzI6rffMGXZQl0LmFO4Nq0uAFAruFjCwb1wQctdR3qqilPHX/4xJRBzzxItY+n29ZK8LkxC8P4EobD6WwwJ95hSYOeG+OIi2IMhgIRKjw=’,‘Recuperacion’);"

As you can see the value of “onclick” is in bold.

In order to get the source code of the webpage that I’m watching on HTMLViewer is the following:
HTMLviewer.ExecuteJavaScript(“window.status = document.getElementsByTagName(‘html’)[0].innerHTML”)

[quote=231502:@Gerardo García]Thanks for post Michel. Well thats what I wanna do, I want to get the attribute “onclick” of the IMG objects, like the following code:

<img id=“BtnDescarga” name=“BtnDescarga” src=“Images/World-download.png” height=“25” width=“25” class=“BtnDescarga” onclick="return AccionCfdi(‘RecuperaCfdi.aspx?Datos=hNDX+PXkv+OISfvZ8Ty8ZCIamIZSmnCcV4mvnvLFoH9oR9CpdR3HooNSGW2+f6xA5sw8AxTqJU6Zv8eVjgxo0V0RH0cZSXeMokaQeP9AFS4kig1oY4z78jqYAxvDQiFaVCHJsU1ORg/SYc2EObQ3pEjQBbS7MKQkdfi0dRfJL1vG+dfuH+UvRffktruGSIER7ST7LuTdYjC+olsNPfb7TFk6NR1VOEOkVPQ3Uk5J4yJGioTTKtSLYP90z/nA8WRmF/mM1sYQ0eMTgpnEK1W0b5fenLcddyOqvwt244KhS+N0mJILhgNtMQJWLSPrhJrSZJjKpnC5MxXNrr0FfuFMwvTBd7AuJqIuEJ6npcmTwOPOYIfUtnxw42CMrXjjTNy1FFWzI6rffMGXZQl0LmFO4Nq0uAFAruFjCwb1wQctdR3qqilPHX/4xJRBzzxItY+n29ZK8LkxC8P4EobD6WwwJ95hSYOeG+OIi2IMhgIRKjw=’,‘Recuperacion’);"

As you can see the value of “onclick” is in bold.

In order to get the source code of the webpage that I’m watching on HTMLViewer is the following:
HTMLviewer.ExecuteJavaScript(“window.status = document.getElementsByTagName(‘html’)[0].innerHTML”)[/quote]

You have two ways to do what you want to do : in JavaScript I gave you all the pointers above, or get the content of the page into a Xojo string, then use split on img.

That will give you a series of array elements into which you will find your src. Then use Instr() to locate the src content.

Hi men I wrote Window.status instead of ImgelementsArray in order to pass the value to the “StatusChanged” HTMLViewer’s Event.

and then pass the “NewStatus” value to a variable or a String Property. and then use it on a Msgbox or a TextArea.

Well. I probe your code: HTMLViewer1.ExecuteJavaScript(“window.status = document.getElementsByTagName(”“img”“)[0]”) and get this: [object HTMLImageElement]

And when I do this: HTMLViewer1.ExecuteJavaScript(“window.status = document.getElementsByTagName(‘img’);”) I get this: [object NodeList]

I was googling in order to get all the images with javascript and I found this:
var images = document.getElementsByTagName(‘img’);
var srcList = ;
for(var i = 0; i < images.length; i++) {
srcList.push(images[i].src);
}

But i dunno how to write it on Xojo to make it work, I was thinking to this:
'Dim ShowImages As String

'ShowImages = “var images = document.getElementsByTagName(‘img’);”
'ShowImages = ShowImages + “var srcList = ;”
'ShowImages = ShowImages + “for(var i = 0; i < images.length; i++) {”
'ShowImages = ShowImages + “srcList.push(images[i].src);”
'ShowImages = ShowImages + “}”

'visor.ExecuteJavaScript(VerImagenes)

I don’t know If I’m wrong. By other side the idea of parse a string with the source code sounds good!, but I rather to get the values at this way if is possible.

Thanks

[quote=231512:@Gerardo García]Hi men I wrote Window.status instead of ImgelementsArray in order to pass the value to the “StatusChanged” HTMLViewer’s Event.

and then pass the “NewStatus” value to a variable or a String Property. and then use it on a Msgbox or a TextArea.
[/quote]

Gerardo,

It looks as though you are still trying to drive in the night with no lights on. JavaScript is a language you got to learn. Everything is explained in the links I posted. Use the try it feature so you can experiment and better get a grasp of the language before you go googling and experimenting without understanding fully what you are doing.

To get the url of the first img here is what you do :

window.status = document.getElementsByTagName('img')[0].src

Then use a JavaScript For to get all the elements in the array.

If you are not quite ready to learn JavaScript, just get the Dom html innerHTML and parse it in Xojo. Sometimes it is better to stick to what you know. In the end, what counts is the result.

[quote=231527:@Michel Bujardet]Gerardo,

It looks as though you are still trying to drive in the night with no lights on. JavaScript is a language you got to learn. Everything is explained in the links I posted. Use the try it feature so you can experiment and better get a grasp of the language before you go googling and experimenting without understanding fully what you are doing.

To get the url of the first img here is what you do :

window.status = document.getElementsByTagName('img')[0].src

Then use a JavaScript For to get all the elements in the array.

If you are not quite ready to learn JavaScript, just get the Dom html innerHTML and parse it in Xojo. Sometimes it is better to stick to what you know. In the end, what counts is the result.[/quote]
Yes I need to learn Javascript before. Its a good idea :smiley: