flow icon indicating copy to clipboard operation
flow copied to clipboard

onload event not working when loading JavaScript to browser

Open letrthang opened this issue 1 year ago • 4 comments

Description of the bug

Vaadin flow 23, Srping boot Java 17.

Using @JavaScript to load JS file to browser. However onload event does not auto trigger.

Reference code here:

https://github.com/letrthang/vaadin23-flow-pro-components/blob/main/src/main/java/com/example/application/views/helloworld/HelloWorldView.java

image image

Expected behavior

onload event works properly

Minimal reproducible example

https://github.com/letrthang/vaadin23-flow-pro-components/blob/main/src/main/java/com/example/application/views/helloworld/HelloWorldView.java

Versions

  • Vaadin / Flow version: 23
  • Java version: 17
  • OS version:Any
  • Browser version (if applicable):any
  • Application Server (if applicable):N/A
  • IDE (if applicable):N/A

letrthang avatar Jul 23 '22 11:07 letrthang

Note: if try as below solution, it works

image

so, workaround solution is to add JS file to index.html

image

letrthang avatar Jul 23 '22 15:07 letrthang

@letrthang could you please elaborate more on what you want to achieve here? Do you have a JS file called "clientLocation.js" that contains the code to register a listener for load event? Where is it located?

taefi avatar Aug 02 '22 13:08 taefi

@letrthang could you please elaborate more on what you want to achieve here? Do you have a JS file called "clientLocation.js" that contains the code to register a listener for load event? Where is it located?

Hi @taefi , you can check on this file: https://github.com/letrthang/vaadin23-flow-pro-components/blob/main/src/main/java/com/example/application/views/helloworld/HelloWorldView.java

As you see, onload method in file clientLocation.js is not called.

https://github.com/letrthang/vaadin23-flow-pro-components/blob/main/src/main/resources/META-INF/resources/javascript/clientLocation.js

i want load listener is triggered when this file clientLocation.js loaded in browser. But this maybe a limitation of vaadin flow and the workaround above is fine for now.

letrthang avatar Aug 10 '22 14:08 letrthang

i want load listener is triggered when this file clientLocation.js loaded in browser.

The page has been loaded before your clientLocation.js is loaded so the load event has already been fired. It is not clear why you would need an event listener instead of doing window.scrollTo(0, 0); directly in your script

Artur- avatar Aug 11 '22 07:08 Artur-

Hi @letrthang, sorry for delayed answer.

As Artur said, using @Javascript annotation, your script is loaded after the page is loaded.

If you want your script to be part of the index.html you can implement AppShellConfigurator.configurePage(...) to inline it

For example, in your sample project, you can add the following method to the com.example.application.Application class (that implements AppShellConfigurator)

    @Override
    public void configurePage(AppShellSettings settings) {
        settings.addInlineFromFile("META-INF/resources/javascript/clientLocation.js", Inline.Wrapping.JAVASCRIPT);
    }

This configuration produces the same effects as your workaround.

As now we have an example on how to achieve your goal, I'm going to close the issue. Please don't hesitate to reopen it or to create a new one if you still have problems.

mcollovati avatar Sep 13 '22 11:09 mcollovati

Here's also a link to the documentation

https://vaadin.com/docs/latest/advanced/modifying-the-bootstrap-page

mcollovati avatar Sep 13 '22 11:09 mcollovati