Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it ok to use Django Template Tag inside JS script
    primarykey
    data
    text
    <p>I'm implementing Google Maps API and I'd like the first marker's InfoWindow to open when the template is first rendered but <strong>only</strong> if a certain condition is true.</p> <p>I have something like this:</p> <pre><code>{% if project %} //the following is automatically open the infowindow of the FIRST marker in the array when rendering the template var infowindow = new google.maps.InfoWindow({ maxWidth:500 }); infowindow.setContent(markers[0].html); infowindow.open(map, markers[0]); {% endif %} </code></pre> <p>This does <em>not</em> throw an error in Firefox or Internet Explorer 7; it does what I want - but it just SEEMS wrong. My text editor is screaming its head off with warnings/errors.</p> <p>Is this bad coding practice? And if so, any suggestions for the alternative? </p> <hr> <p>This is the complete code, inside script tags, with the irrelevant bits edited out:</p> <pre><code>function initialize() { ... var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); var markers = [] setMarkers(map, projects, locations, markers); ... } function setMarkers(map, projects, locations, markers) { for (var i = 0; i &lt; projects.length; i++) { var project = projects[i]; var address = new google.maps.LatLng(locations[i][0],locations[i][1]); var marker = new google.maps.Marker({ map: map, position:address, title:project[0], html: description }); markers[i] = marker; google.maps.event.addListener(marker, 'click', function() { infowindow.setContent(this.html); infowindow.open(map,this); }); } {% if project %} //the following is automatically open the infowindow of the FIRST marker in the array when rendering the template var infowindow = new google.maps.InfoWindow({ maxWidth:500 }); infowindow.setContent(markers[0].html); infowindow.open(map, markers[0]); {% endif %} }) } google.maps.event.addDomListener(window, 'load', initialize); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload