Project

General

Profile

JavaScript and page refresh. Race condition?

Added by David Kauderer over 6 years ago

Hello,

I'm building a WT application, that uses a couple of custom java scripts.

I include them with

WApplication::instance()->require("resources/js/myjs.js");

and I execute them with

WWidget::dojavaScript();

So far so good. Everything works...UNTIL I press F5 or refresh via Button in the browser. Afterwards the JS is not executed anymore.

I already searched in the Forum, but didn't really find a suitable answer.

Has anyone similar experiences and can help me? It would be really appreciated.

Best, David


Replies (6)

RE: JavaScript and page refresh. Race condition? - Added by Mark Petryk over 6 years ago

We have some 'require' java, but we have not experienced an no-load on F5.

I would recommend that you inspect your browser after F5, specifically on the network tab to see what's getting called out, what's loading, what's not loading and so on...

Our site is here:

https://fundandgrow.com/

And the BBB logo down at the bottom employs the 'require' java, and it does not suffer from F5 race.

RE: JavaScript and page refresh. Race condition? - Added by David Kauderer over 6 years ago

Hello Mark,

thank you for your answer. When I inspect the site, I can see, that my javascript has been loaded. It's just not executed.

Therefor I think it's executed, but just before everything has been loaded and therefor the execution fails.

As a workaround I have overridden the WWidget::refresh() function now and execute the JS again there.

This seems to work, but I don't know, if this is a good solution.

Best, David

RE: JavaScript and page refresh. Race condition? - Added by Mark Petryk over 6 years ago

Ok, then I am not knowing... I would expect the java to run only after everything else is loaded.

Can you paste your relevant codes here?

RE: JavaScript and page refresh. Race condition? - Added by David Kauderer over 6 years ago

Hello Mark,

The project is very huge. I cannot paste the code here. But here a snippet:

I'm using the auth2 example from WT and after authentification, I create a DisplayModule object and add it to root().

The require() I added to the constructor of authapp.

@

DisplayModule::DisplayModule(Session *session):

WContainerWidget(){

[...]

this->doJavaScript("widgetScroll()");

[...]

}

Like this, JS fails after pressing F5. After adding this:

void DisplayModule::refresh() {

this->doJavaScript("widgetScroll()");

}@

it works allright.

Thanks, that you try to help.

Best, David

RE: JavaScript and page refresh. Race condition? - Added by Wim Dumon over 6 years ago

David,

What js library are you requiring? What error do you get in the browser's console when the doJavaScript() is executed?

Wt waits until the JS library is loaded to execute the doJavaScript, but some JS libraries load themselves asynchronously and require you to wait for some 'loaded' signal before you can use them, which may cause the behaviour you describe.

BR,

Wim.

RE: JavaScript and page refresh. Race condition? - Added by Michael Spellman almost 6 years ago

I think that what might be happening is the same as what is happening to me:

One of my widgets requires some special JavaScript to setup some DOM elements inside a WContainerWidget; if I only put my `doJavaScript` call into my constructor, it works the first time, but refreshing results in a blank div container; but when I place the code into `refresh` as well, it works.

What is happening is that the Widget object is initialized on the server and the javascript is called on the first page load, so the client side is initialized; but when the page refresh (f5) happens, then the initialized widget is still valid on the server and it does not run the constructor again, so the JavaScript is not called to initialize it, so the browser and the server are out of sync in the initialization, but if you also run the JavaScript in `refresh()`, it is run every time and re-initializes the div contents.

Hope that helps!

    (1-6/6)