I have a reusable google-maps widget that can be plugged into any page that might want to use it.
Widget has :
- Google maps specific
jsincludes. - Map specific
cssto style the map.
Problem:
- Widget code gets included as
server-sideinclude. - Widget specific
jsandcsshas to included with the widget code. - Therefore, if widget repeats 5 times on a page, then the include statements for
jsandcssget included 5 times. - I can-not keep the widget specific includes in the
<head>tag, since if the widget is not used on the page, then un-usedjsandcssgets downloaded on client.
So, how should I keep the js and css so they gets included only when the widget is used and no multiple include are done in case the widget is repeated.
My technology:
Django-Python[ If that matters ]- I also faced the same problem on
php
Note :
- I understand that including the same
jsandcssmultiple times on a page doesn’t forces the client to download them multiple times, but I want to avoid the multiple includes.
How should I tackle this problem ?
Please feel free to ask for more info, if I missed something. 🙂
Depending on your use case a few options:
Use a separate base template for all pages that need the widget; in that base add the required js/css files. For all views that need the widget, inherit from the modified base.
Using javascript implement conditional includes.