Browse Source

test javascript

testing
Burathar 4 years ago
parent
commit
db9dfd0964
  1. 38
      app/templates/player_base.html

38
app/templates/player_base.html

@ -8,16 +8,20 @@
{% block scripts %} {% block scripts %}
{{ super() }} {{ super() }}
<script> <script>
var csrftoken = $('meta[name=csrf-token]').attr('content') $(document).ready(sendLocation)
$.ajaxSetup({
beforeSend: function(xhr, settings) { function sendLocation(){
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) { if (navigator.geolocation) {
xhr.setRequestHeader("X-CSRFToken", csrftoken) navigator.geolocation.getCurrentPosition(postCoordinates, showError);
}
} else {
console.log('Geolocation is not supported by this browser.')
} }
}) }
setInterval(function() {sendLocation()}, 10 * 1000);
function sendLocation(position) { function postCoordinates(position) {
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: "{{ url_for('main.send_location', username=current_user.name) }}", url: "{{ url_for('main.send_location', username=current_user.name) }}",
@ -32,6 +36,15 @@ function sendLocation(position) {
}); });
} }
var csrftoken = $('meta[name=csrf-token]').attr('content')
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken)
}
}
})
function showError(error) { function showError(error) {
switch(error.code) { switch(error.code) {
case error.PERMISSION_DENIED: case error.PERMISSION_DENIED:
@ -50,14 +63,5 @@ function showError(error) {
} }
} }
$(document).ready(function(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(sendLocation, showError);
} else {
console.log('Geolocation is not supported by this browser.')
}
})
</script> </script>
{% endblock %} {% endblock %}
Loading…
Cancel
Save