为什么我的剧本表现不好
Why is my script not behaving properly?
我编写了一个函数,可以查找用户位置并相应地更改链接。功能如下:
/*
This function is to determine the users location and integrate that data into the navigation aspect
*/
function findLocation() {
try { // try to find the users location and then manipulate it to get the correct google maps query strings/links
var destinations = { // destinations to go to: to be added to the query string
bridgeFromUs: "Ambassador+Bridge,+Detroit,+MI",
bridgeFromCa: "Ambassador+Bridge,+Ambassador Bridge,+Windsor,+ON,+Canada",
tunnelFromUs: "Detroit+Windsor+Tunnel,+Detroit,+MI",
tunnelFromCa: "The+Windsor+Detroit+Tunnel,+Windsor,+ON,+Canada"
};
// the following variables are the correct variable for the query string
var bridgeAddress;
var tunnelAddress;
// switch through the values for the country variable -- it is declared in the head of each HTML page
switch (country) {
case "CA": // if it is for Canada, reflect that in the query strings
bridgeAddress = destinations.bridgeFromCa;
tunnelAddress = destinations.tunnelFromCa;
break;
case "US": // if it is for the US, reflect that in the query strings
bridgeAddress = destinations.bridgeFromUs;
tunnelAddress = destinations.tunnelFromUs;
}
// now the preliminary work is done -- let's find out where they are
navigator.geolocation.getCurrentPosition(
function (position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
// store the position for the session -- VERY unnecessary, but it is cool and HTML5! ;)
sessionStorage.setItem('latitude', latitude);
sessionStorage.setItem('longitude', longitude);
// link for where they are
var addr = "https://maps.google.com/maps?f=d&source=s_d&saddr=" + latitude + ",+" + longitude + "&daddr=";
console.log(addr);
return addr;
},
function createLink(addr) {
var baseLink = arguments[0];
// final links
var link = {
bridge: baseLink + bridgeAddress,
tunnel: baseLink + tunnelAddress
};
// set them in the DOM
document.getElementById('bridgeLink').href = link.bridge;
document.getElementById('tunnelLink').href = link.tunnel;
}
);
// if we fail...
} catch (e) {
// send an alert...
alert("Hey! We can't find out where you are! You should check back later.");
// ...and set some links anyway.
var lin = "http://maps.google.com";
document.getElementById('bridgeLink').href = lin;
document.getElementById('tunnelLink').href = lin;
}
}
我知道这个函数正在运行,因为它是console.log
,而addr
变量正好。但它并没有改变链接的地址。链接仍然设置为我设置的默认"#"
。我尝试过使用控制台进行调试,但没有显示任何错误。这是怎么回事?
以下是问题的链接:http://54.201.70.251/static/canada.html
createLink应该只在函数(位置)中调用,现在您将createLink作为错误回调传递。
navigator.geolocation.getCurrentPosition(success, error
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 知道为什么我的旋转木马不会自动更改图片吗
- 为什么我的d3.jsselectAll+过滤器没有过滤
- 为什么不't我的变量在我的控制器中填充后在我的视图中呈现
- 为什么我的点击事件没有使用 react js 触发
- 为什么我的JavaScript堆栈排序函数不起作用
- 为什么使用immutableJS我的状态没有改变
- Don'我不明白为什么我的setInterval+jQuery;不起作用
- 如何在angular js中使用指令.不适用于我的情况,为什么
- 我的jquery代码不起作用.为什么?
- 为什么不't Express呈现我的React应用程序
- 为什么我的按钮组件提交它所在的表单
- 为什么不是'我的JS滑块正在工作
- 为什么不是'我的函数在解析云代码中工作吗?当我在Angular和Express中测试时,它是有效的
- 为什么不'当单元测试出现解析错误时,我的因果报应测试会失败
- 我不知道为什么我的代码是错误的?又有什么错
- 为什么我的While循环不起作用
- 为什么我的控制器没有启动函数.js脚本
- 为什么我的上下文选择器和.buttonset()在ie中花费了这么长时间
- 为什么我的函数没有被调用呢