B4J Question Difference in JS click() to mouseclick

Patent

Member
Licensed User
Longtime User
Hi community,

wanna ask whats the difference (in a B4J WebView) in a real Mouseclick on a Button to one fired programmatically with

B4J:
Dim a as String = $"document.getElementsByClassName("myButton")[0].click();"$

myWebengine.RunMethod("executeScript", Array As String(a))


If i am clicking with the mouse, some Scripts are running and doing something nice.
With "click" this is not happening.
Are there different Events or different Event-Bubbling things?

I cant give an real example with the scripts and the HTML because its all "very secret" :mad:

Thanks for an hint
Patent
 

Quandalle

Member
Licensed User
The HTMLElement.click() method simulates a mouse click on an element.

When click() is used with supported elements (such as an <input>), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.


So there is probably an error in your program, but without a "real" example, it is difficult to help you.

To give you two directions for debugging you can :

1) Replace the script in the line
B4X:
Dim a as String = $"document.getElementsByClassName("myButton")[0].click();"$
with something like :
B4X:
 Dim a as string = $"alert("**button click**");"$
and then check the alert box is displayed when you call the script with the "excuteScript" method

2) after loading your page in the browser, go to the console (F12 and console menu) and type your line:
B4X:
document.getElementsByClassName("myButton")[0].click();
and check that the execution occurs. If it doesn't, it means that the writing is bad, and correct it.
 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
B4X:

search:
 
Upvote 0

Patent

Member
Licensed User
Longtime User
thanks to you all for hinting me :) (and not hitting 😂).

The Problem was not the B4X Code, because there is no, except the code in Post #1.

Origin Question was:
-If i am clicking with the mouse, some Java-Scripts are running and doing something nice.
-With "click" this is not happening.
@Quandalle: sorry, i wasnt exact enough: 'click' also fires, but it is doing other things!

My new knowledge:
-you cant assume, that 'click' works in a WebPage in the same way as a real click (in most cases yes, but not always).
The (secret!😱 and loong and many!) scripts in my Homepage have changed this behavior: needs mousedown AND click 😤 and than its like a real mouseclick!

My working solution:
B4X:
Dim s As String=$"
        var e1 = jQuery.Event( "mousedown");
        var e2 = jQuery.Event( "click");
        var    x = document.getElementsByClassName("myElement")[0];
        
        jQuery(x).trigger(e1);
        jQuery(x).trigger(e2);
    "$
    
    myWebengine.RunMethod("executeScript", Array As String(s))

So, maybe useful for others

thanks
many greets
 
Upvote 0

Quandalle

Member
Licensed User
@Patent : "sorry, i wasnt exact enough: 'click' also fires, but it is doing other things!"

No, no, ...When click() is used with an element it fires the element's click event. an do nothing else like firing "mousedown" as you indicated.

the problem is that in your question you talk about MouseClick (which is a javascript event), while you should have talked about the action : "click on a button

Clicking a button generates 3 events:
  • mousedown
  • mouseup
  • click

The code of the page you are using, seems to have two event handlers installed: mousedown, and click. (also check if your page does not handle mouseup)
So to simulate the action of clicking on the button you have to simulate the two events : mousedown, and click

the code you posted with Jquery simulates these 2 events. It is better in 2022 to remove the use of jquery and to do this in pure javascript.

for example pure javascript code
B4X:
let elt=document.getElementsByClassName("myButton")[0];
elt.dispatchEvent(new Event("mousedown"));
elt.dispatchEvent(new Event("click"));
 
Upvote 0
Top