Call JS / CSS everytime i open field.yaml / form page, and sometimes error form page refreshed twice calling JS twice

im using octobercms version 3 latest version
im trying to create a google map on formwidget

public function loadAssets()
{
    $this->addJs( xxxx );
}
  1. if im using loadAsset and $this->addJS its only called once if loaded
    example menu “map” → create new map
    the page loaded fine, my google map showed up

if i try to update the record or create new map, the google map not showed up cause its only loaded the JS once

==============================================
2. so, i try to put js script in my widget partial.htm

if i refresh my form page its show the google map
but if im back to menu “map” then try to create new again

the page blinking then loaded / refreshed twice ,
my google map script called twice and its showing error

“You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors.”

is there a solution for my problem ?
i need the JS to be called once everytime i open the form page either for create / update

Hi and welcome @razarulz,

This is caused by the Turbo Router, part of October CMS’ AJAX framework. There are a couple of considerations to use when writing JavaScript:

One thing to try is disable the subsequent eval/loading of the scripts using the data-turbo-eval="false" tag: Turbo Router - October CMS - 3.x

public function loadAssets()
{
    $this->addJs( xxxx , ['data-turbo-eval' => 'false']);
}

Optimally, you should try to make the control idempotent, a concept described more in this section: Turbo Router - October CMS - 3.x

I hope this helps.

1 Like

thanks, its works now
ive been trying it for hours and didnt expect it in documentation ajax

1 Like