簡體   English   中英

Javascript,生成結果到文本文件

[英]Javascript, generate result to text file

這是來自http://locationdetection.mobi的示例 javascript 代碼,用於使用 google API 檢測地理位置。 (原始 zip 文件包含一個 php 文件、html 和此 javascript 代碼)

正如您在下面的代碼中看到的,在此 javascript 代碼的最后一部分,有一行代碼將位置檢測的結果呈現到 html 文件。
如何將結果生成到文本文件而不是渲染到瀏覽器?

// this is called when the browser has shown support of navigator.geolocation
function GEOprocess(position) {
  // update the page to show we have the lat and long and explain what we do next
  document.getElementById('geo').innerHTML = 'Latitude: ' + position.coords.latitude + ' Longitude: ' + position.coords.longitude;
  // now we send this data to the php script behind the scenes with the GEOajax function
  GEOajax("geo.php?accuracy=" + position.coords.accuracy + "&latlng=" + position.coords.latitude + "," + position.coords.longitude +"&altitude="+position.coords.altitude+"&altitude_accuracy="+position.coords.altitudeAccuracy+"&heading="+position.coords.heading+"&speed="+position.coords.speed+"");
}

// this is used when the visitor bottles it and hits the "Don't Share" option
function GEOdeclined(error) {
  document.getElementById('geo').innerHTML = 'Error: ' + error.message;
}

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(GEOprocess, GEOdeclined);
}else{
  document.getElementById('geo').innerHTML = 'Your browser sucks. Upgrade it.';
}

// this checks if the browser supports XML HTTP Requests and if so which method
if (window.XMLHttpRequest) {
  xmlHttp = new XMLHttpRequest();
}else if(window.ActiveXObject){
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

// this calls the php script with the data we have collected from the geolocation lookup
function GEOajax(url) {
 xmlHttp.open("GET", url, true);
 xmlHttp.onreadystatechange = updatePage;
 xmlHttp.send(null);
}

// this reads the response from the php script and updates the page with it's output
function updatePage() {
 if (xmlHttp.readyState == 4) {
  var response = xmlHttp.responseText;
  document.getElementById("geo").innerHTML = '' + response;
 }
}

您不能從前端創建文本文件,至少不能在瀏覽器中配置一些標志,因此您需要將數據發送到后端語言,創建文件然后下載

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM