Ric*_*Coy 6 javascript hash jquery google-chrome
我正在使用以下代码段,如果jQuery JavaScript在URL的末尾返回哈希值.它在FF中完美运行,但第4行的警报在Chrome中返回空.
好像这window.location.hash.substring(1)条线不起作用.我也试过了window.location.hash.replace("#", "");
// Map Clicks
$("#tab2.tab_content #map").delayed('click', 500, function() {
state = window.location.hash.substring(1);
alert(state);
jsonLink = 'http://ml.uscm.org/ministries.json?state=' + state + '&active=true&callback=?';
getMapResults();
return false;
});
Run Code Online (Sandbox Code Playgroud)
从Chrome中的URL检索哈希值的诀窍是什么?
URL构建如下:
http://www.ourdomain.com/thispage.html#IL
Run Code Online (Sandbox Code Playgroud)
IL是一个州的两个字母缩写.我想得到"IL".
我在这里有一个工作演示:
http://richcoy.com/locator/index2.html
点击Chrome中的"按州搜索"标签,然后点击状态,您就会看到问题所在.浏览器显示它要转到的URL正确构建. -
谢谢.
您可能想尝试这样做:
$(window).on('hashchange', function() {
console.log(window.location.hash.substring(1));
});
Run Code Online (Sandbox Code Playgroud)
click事件在hashchange事件之前触发,因此您无法依赖地图点击工具(即使您将其延迟).
支持hashiki的浏览器列表:http://caniuse.com/hashchange
如果您不必使用哈希,这是一个更简单的解决方案:
$("#tab2.tab_content #map a").click(function() {
console.log($(this).attr('href').substring(1));
});
Run Code Online (Sandbox Code Playgroud)
总之,您不应使用任何类型的延迟方法.
我感谢所有的帮助和建议。
问题的答案最终是我需要将 URL 的路径设置为绝对路径,而不是相对路径。
举个例子,JavaScript 中的一行来自:
"AL": {tooltip: 'Alabama', attr: {fill: '#F9B625', href: '#AL'}},
Run Code Online (Sandbox Code Playgroud)
到:
"AL": {tooltip: 'Alabama', attr: {fill: '#F9B625', href: 'http://richcoy.com/locator/index2.html#AL'}},
Run Code Online (Sandbox Code Playgroud)
当我为所有 50 个州更改此设置后,单击地图即可正确从 jasonp 提要中提取数据,并将其显示在 Chrome 和 Firefox 的页面上。
| 归档时间: |
|
| 查看次数: |
1425 次 |
| 最近记录: |