0

Hi i'm having a problem with inclusion of javascripts specifically Google maps API.

I have a page for example that includes the library:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places,visualization"></script>

//some page contents

$this->widget('MyWidgetThatIncludesGoogleAPI',array(
..
));

The problem arises when I call my widget which also includes google maps api, and even if I don't initially include the maps api in this page. Calling the widget twice does the same.

I get an error saying that Google Maps API has been loaded multiple times, is there a programmatic way to handle duplicate inclusion of js?

2
  • don't want to include it twice? then don't include it twice Commented Oct 14, 2014 at 2:16
  • as i've mentioned in my post; even if i don't include it, calling a widget twice, or even doing a renderPartial() to a page having it causes the inclusion fo the js twice. Commented Oct 14, 2014 at 2:37

1 Answer 1

1

You could use a global variable to track whether it has already been included. You could then create a function to include the script if it hasn't already been included.

$includedGoogleMaps = false;
function includeGoogleMaps() {
    global $includedGoogleMaps;
    if (!includedGoogleMaps) {
        echo '<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places,visualization"></script>';
        $includeGoogleMaps = true;
    }
}

The function isn't exactly necessary but it makes everything a lot cleaner.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.