1

Is there an easy way to check if the current page is the Dashboard? I have a widget that needs several Javascript resources but if I include them in the widget library I get a google maps error that I should not do that. You have included the Google Maps API multiple times on this page. This may cause unexpected errors.

I could make a workaround and set a static public variable to false after the first include but that would be kinda dirty.

So all I want to know id there is something like

function init()
{
    if (craft()->isDashBoard()) {
        //include all JS ressources
    }
}

or if there is a better way to include Javascript.

3

Nope, but you can use the {% includeJsFile %} tag to include the file(s) from your widget template. In whatever template you render in your widget's getBodyHtml() method, add this:

{% includeJsFile "https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY" %}

If you need to queue up several files, you'll need several {% includeJsFile %} tags. There's also a {% includeCssFile %}, if you need to add some external CSS, as well.

1

You know that init() would fire everytime an action is done, so you would load multiple google map apis it is mostly used for hooking into Events.

I would try something else to load it in.

Control Panel JS plugin for craft and then by there load in your javascript. <- works good if i remember correctly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.