// JavaScript Document
// geolocation.js
function getCoordinates()
{
var options = {
enableHighAccuracy: true, //这个参数不影响精度
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var coords = pos.coords;
console.log(coords);
console.log(pos);
console.log('Your current position is:');
console.log('Latitude : ' + coords.latitude);
console.log('Longitude: ' + coords.longitude);
console.log('More or less ' + coords.accuracy + ' meters.');
};
function error(err) {
console.log('ERROR(' + err.code + '): ' + err.message);
};
navigator.geolocation.getCurrentPosition(success,error,options);
}
function gtCooordinates()
{
function showPostion(position)
{
console.log(position.coords);
}
navigator.geolocation.getCurrentPosition(showPostion);
}
var geoinfo=new Object();
/*
http://ifconfig.me/ip
http://ipinfo.io/ip
https://ifconfig.co/ip
*/
function getgeoinfo()
{
var urls=new Array('http://ifconfig.me/ip','http://ipinfo.io/ip');
var xhr=new XMLHttpRequest;
xhr.open('get',urls[Math.floor(Math.random()*urls.length)]);
xhr.onreadystatechange=function()
{
if(xhr.readyState==4&&this.status==200)
{
if(xhr.response!=''&&xhr.response!=null)
{
try
{
sessionStorage.setItem('transportbuf',xhr.response.replace(/\s*/g,""));
//console.log(xhr.response.replace(/\s*/g,""));
if(geoinfo==null)
{
geoinfo=new Object();
}
geoinfo.ipv4=sessionStorage.getItem('transportbuf');
localStorage.setItem('geoinfo',JSON.stringify(geoinfo));
function showPostion(position)
{
geoinfo.latitude=position.coords.latitude;
geoinfo.longitude=position.coords.longitude;
localStorage.setItem('geoinfo',JSON.stringify(geoinfo));
}
navigator.geolocation.getCurrentPosition(showPostion);
//console.log(geoinfo);
}
catch(err)
{
console.log(xhr.response);
console.log(xhr.responseText);
console.log(sessionStorage.getItem('transportbuf'));
throw err;
}
}
}
}
xhr.withCredentials=false;
try
{
xhr.send(null,true);
}
catch(err)
{
console.log(xhr.responseURL);
console.log(err);
}
}
function getgeoinfo(fn)
{
var urls=new Array('http://ifconfig.me/ip','http://ipinfo.io/ip');
var xhr=new XMLHttpRequest;
xhr.open('get',urls[Math.floor(Math.random()*urls.length)]);
xhr.onreadystatechange=function()
{
if(xhr.readyState==4&&this.status==200)
{
if(xhr.response!=''&&xhr.response!=null)
{
try
{
sessionStorage.setItem('transportbuf',xhr.response.replace(/\s*/g,""));
//console.log(xhr.response.replace(/\s*/g,""));
//console.log(sessionStorage.getItem('transportbuf'));
if(geoinfo==null)
{
geoinfo=new Object();
}
geoinfo.ipv4=sessionStorage.getItem('transportbuf');
localStorage.setItem('geoinfo',JSON.stringify(geoinfo));
function showPostion(position)
{
geoinfo.latitude=position.coords.latitude;
geoinfo.longitude=position.coords.longitude;
localStorage.setItem('geoinfo',JSON.stringify(geoinfo));
}
navigator.geolocation.getCurrentPosition(showPostion);
if(fn!=null && typeof fn == 'function')
{
//console.log('success function',geoinfo);
fn();
}
}
catch(err)
{
console.log(xhr.response);
console.log(xhr.responseText);
console.log(sessionStorage.getItem('transportbuf'));
throw err;
}
}
}
}
xhr.withCredentials=false;
try
{
xhr.send(null,true);
}
catch(err)
{
console.log(xhr.responseURL);
console.log(err);
}
}
// JavaScript Document
// ip.js
window.onload=getpublicIp;
function getpublicIp()
{
//xhr无法使用https 'https://ifconfig.co/ip'
var urls=new Array('http://ifconfig.me/ip','http://ipinfo.io/ip');
var xhr=new XMLHttpRequest;
xhr.open('get',urls[Math.floor(Math.random()*urls.length)]);
xhr.onreadystatechange=function()
{
if(xhr.readyState==4&&this.status==200)
{
if(xhr.response!=''&&xhr.response!=null)
{
try
{
if(xhr.responseText!=null && xhr.responseText!='')
localStorage.setItem('ip',xhr.responseText);
}
catch(err)
{
console.log(xhr.response);
throw err;
}
}
}
}
xhr.withCredentials=false;
try
{
xhr.send(null,true);
}
catch(err)
{
console.log(xhr.responseURL);
console.log(err);
}
}