地理位置允许位置异常导致站点无法进入成功功能 - javascript

这是我的地理位置代码

<!-- <xml version="1.0" encoding="utf-8"> -->
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Geolocation API Demo</title>
<meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" name="viewport"/>
<script>
function successHandler(location) {
    var message = document.getElementById("message"), html = [];
    html.push("<img width='400' height='400' src='http://maps.google.com/maps/api/staticmap?center=", location.coords.latitude, ",", location.coords.longitude, "&markers=size:small|color:blue|", location.coords.latitude, ",", location.coords.longitude, "&zoom=14&size=256x256&sensor=false' />");
    html.push("<p>Longitude: ", location.coords.longitude, "</p>");
    html.push("<p>Latitude: ", location.coords.latitude, "</p>");
    html.push("<p>Accuracy: ", location.coords.accuracy, " meters</p>");
    message.innerHTML = html.join("");

    var javascriptLongitude = location.coords.longitude;
    var javascriptLatitude = location.coords.latitude;
    var javascriptAccuracy = location.coords.accuracy;
    window.location.href = "myphpfile.php?longitude=" + javascriptLongitude + "&" + "latitude=" + javascriptLatitude + "&" + "accuracy=" + javascriptAccuracy;
}
function errorHandler(error) {
    alert('Attempt to get location failed: ' + error.message);
}
navigator.geolocation.getCurrentPosition(successHandler, errorHandler);

</script>
</head>
<body>
<a href="http://jquery.com/">jQuery</a>
    <script src="jquery.js"></script>
<div id="message">Location unknown</div>
</body>
</html>

我在这4行中设置了变量

var javascriptLongitude = location.coords.longitude;
var javascriptLatitude = location.coords.latitude;
var javascriptAccuracy = location.coords.accuracy;
window.location.href = "myphpfile.php?longitude=" + javascriptLongitude + "&" + "latitude=" + javascriptLatitude + "&" + "accuracy=" + javascriptAccuracy;

这是myphpfile.php代码

<?php
$Longitude = $_GET["longitude"];
$Latitude = $_GET["latitude"];
$Accuracy = $_GET["accuracy"];
$Return = " ".PHP_EOL;

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
fwrite($myfile, $Longitude);
fwrite($myfile, $Return);
fwrite($myfile, $Latitude);
fwrite($myfile, $Return);
fwrite($myfile, $Accuracy);
fclose($myfile);
?>

我现在的主要问题是当我打开网站时,它询问是否允许它知道我的位置,然后单击允许,它可以工作一次……然后当我关闭选项卡并再次打开它时,它不起作用并向我显示代码中所示的未知位置,要解决此问题,我必须点击
地理位置允许位置异常导致站点无法进入成功功能 - javascript

并且我必须单击“清除这些设置以备将来访问”,或转到chrome:// settings / contentExceptions#location并从此处删除我网站的项目,然后它将再次起作用...而我猜这无法通过手机完成...

我的第二个问题,实际上没什么大不了,当写入txt文件时,“ \ n”,“ \ r \ n”和PHP_EOL不起作用,尝试将它们添加为$Longitude = $_GET["longitude"].PHP_EOL;$Longitude = $_GET["longitude"]."\n";没运气

参考方案

似乎是jquery.js标记阻止了dom元素的加载。另外,请确保检查浏览器是否确实内置了此功能,因为许多功能仍然没有。

`

<div id="message">Loading...</div>

`

function successHandler(location) {
   var message = document.getElementById("message"),
     html = [];
   html.push("<img width='400' height='400' src='http://maps.google.com/maps/api/staticmap?center=", location.coords.latitude, ",", location.coords.longitude, "&markers=size:small|color:blue|", location.coords.latitude, ",", location.coords.longitude, "&zoom=14&size=256x256&sensor=false' />");
   html.push("<p>Longitude: ", location.coords.longitude, "</p>");
   html.push("<p>Latitude: ", location.coords.latitude, "</p>");
   html.push("<p>Accuracy: ", location.coords.accuracy, " meters</p>");
   message.innerHTML = html.join("");

   //Uncomment it for it to redirect.
   //window.location.href = "myphpfile.php?longitude=" + location.coords.longitude + "&" + "latitude=" + location.coords.latitude + "&" + "accuracy=" +  location.coords.accuracy;
 }

 function errorHandler(error) {
   alert('Attempt to get location failed: ' + error.message);
 }
 if (navigator.geolocation) {
   navigator.geolocation.getCurrentPosition(successHandler, errorHandler);
 } else {
   alert("Please download a modern browser.");
 }

`

https://jsfiddle.net/iain17/ratzv34t/

Telerik单选按钮所需的字段验证器 - javascript

如何设置Telerik单选按钮所需的字段验证器?我想在按钮单击“ BtnSave”上设置必填字段验证器吗?请帮忙!<telerik:RadButton ID="radio_male" runat="server" ToggleType="Radio" AutoPostBack="fa…

表单不提交或按钮提交不起作用 - javascript

据我所知,此代码必须有效,但是我编码时却无效问题在于该表单未提交。我该如何解决?选中时,我什至没有得到复选框的值。<table id="example" class="display" cellspacing="0" width="100%"> <thead&g…

在PHP文件中调用javascript函数并在加载HTML文件之后? - javascript

我需要在我的php中调用js函数,但无法正常工作。有人可以告诉我我在做什么错吗?我该如何轻松地做到这一点?谢谢!我有三个文件:  mail.php负责发送$ _POST的内容(工作正常)。我调用我的javascript函数来切换模式,具体取决于邮件是否已发送。 <? ... $response = $sendgrid->send($email);…

如何使用PHP从动态输入字段捕获数组值? - javascript

我正在编写一个在线时间跟踪网页,允许用户将学习时间输入该系统。用户将首先输入名称,然后根据日期输入学习时间。一天中可能会有多个学习时间。以下是我第一页的编码,<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"…

使用php重新加载内容 - javascript

在对网站进行编程时,我以前使用过此代码,它可以完美工作,但是现在当我想使用一些Flash部件时,每次单击链接时,它都会重新加载所有网站。源代码: <!DOCTYPE html> <html> <head> <title>Hot King Staff</title> <meta charset=…