javascript - Is it ok to use Django Template Tag inside JS script - Stack Overflow

I'm implementing Google Maps API and I'd like the first marker's InfoWindow to open when

I'm implementing Google Maps API and I'd like the first marker's InfoWindow to open when the template is first rendered but only if a certain condition is true.

I have something like 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 %}

This does not 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.

Is this bad coding practice? And if so, any suggestions for the alternative?


This is the plete code, inside script tags, with the irrelevant bits edited out:

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 < 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);

I'm implementing Google Maps API and I'd like the first marker's InfoWindow to open when the template is first rendered but only if a certain condition is true.

I have something like 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 %}

This does not 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.

Is this bad coding practice? And if so, any suggestions for the alternative?


This is the plete code, inside script tags, with the irrelevant bits edited out:

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 < 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);
Share Improve this question edited May 12, 2015 at 9:31 beeglebug 3,5421 gold badge24 silver badges40 bronze badges asked Oct 28, 2010 at 18:50 MonkeyBooMonkeyBoo 1472 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

There's nothing wrong with using a Django template tag inside a clump of Javascript. Django's templating language is for the most part language-agnostic: it doesn't care what the templated text means.

I've got rivers of Javascript with tons of tags, conditionals, variable substitutions, and so on.

Your other option for this sort of thing is to insert a Javascript boolean variable, and put the conditional in Javascript:

<script>
var is_project = {% if project %}true{% else %}false{% endif %};

//...

if (is_project) {
    // stuff for project
}
</script>

I suppose this keeps the Javascript cleaner. You'd have to decide based on your own code which style you prefer.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745561755a4633147.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信