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;
});

在 Chrome 中从网址中检索哈希值的诀窍是什么?

网址是这样构建的:

http://www.ourdomain.com/thispage.html#IL

其中 IL 是状态的两个字母缩写。我想得到"IL"。

我在这里有一个工作演示:

http://richcoy.com/locator/index2.html

点击Chrome中的按州搜索标签,然后点击一个州,您将看到问题。浏览器显示它要转到的 url 已正确构建。–

谢谢。

您可能想尝试一下:

$(window).on('hashchange', function() {
    console.log(window.location.hash.substring(1));
});
点击事件

在哈希更改事件之前触发,因此您不能依赖地图点击实现(即使您延迟了它(。

哈希更改支持的浏览器列表:http://caniuse.com/hashchange

如果您不必使用哈希,这里有一个更简单的解决方案:

$("#tab2.tab_content #map a").click(function() {
    console.log($(this).attr('href').substring(1));
});

总之,您不应该使用任何类型的延迟方法。

问题不是很明显,还是我很傻?在下面的函数中,你处理地图上的点击,你听click,延迟它们500ms,然后运行你的函数。

$("#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;
});

但是在你alert你的state点,它是空的,因为你还没有设置它。您的return false;还将阻止浏览器更改哈希。

试试这个功能,我敢打赌你会得到一些东西:

$("#tab2.tab_content #map").delayed('click', 500, function() {
  setTimeout(function(){
    var state = window.location.hash.substring(1);
    alert(state);
    jsonLink = 'http://ml.uscm.org/ministries.json?state=' + state + '&active=true&callback=?';
    getMapResults();
  }, 500);
});

使其正常工作的一种快速方法是执行以下操作:

$("#tab2.tab_content #map a").delayed('click', 500, function(e) {
  state = $(this).attr('href').replace('#', '');
  alert(state);
});

然后,您可以通过将其添加到回调中来轻松手动更改哈希:

window.location.hash = state;

但是您真正的问题是您的a(锚点(本身并没有更改哈希,这使得您的代码中似乎有某个地方阻止了它。

在 Chrome 中,只能设置哈希将锚定,例如:

<a href="#exemplo" />

如果通过设置另一个元素的哈希来执行此操作,请尝试将其交换为锚点。

在此链接中,您会看到 Chrome 接受哈希属性:
http://www.w3schools.com/jsref/prop_loc_hash.asp

你稍微

修改一下你的代码怎么样,

$("#tab2.tab_content #map a").click(function(){
 console.log($(this).attr("href"));//this outputs the value to the console
});

这将为加利福尼亚州输出 #CA

$("#tab2.tab_content #map a").delayed('click', 500, function() {
    //state = window.location.hash.substring(1);
state = $(this).attr("href").substring(1);
        alert(state);
    jsonLink = 'http://ml.uscm.org/ministries.json?state=' + state + '&active=true&callback=?';
    getMapResults();
    return false;
    });

在代码中,如果在触发事件时放置断点 (ln36 state = window.location.hash.substring(1);(,则窗口尚未更改位置,因此该点不存在哈希。

当我检查 Chrome 的检查器时,您的锚点似乎使用了来自不同命名空间的href,该命名空间未在您的 svg 标记中声明。

<a xlink:href="#SD">

Firefox似乎对此很好,但Chrome似乎没有猜测如何正确解释这一点。

从这个链接中,我发现:

绑定所需的命名空间

SVG 是一种命名空间的 XML 方言,因此所有 XML SVG 文档中使用的方言必须绑定到其命名空间 如 XML 中的命名空间建议中所述。这是明智的 至少绑定 SVG 和 XLink 命名空间,也可能绑定 命名空间。

因此,请尝试将xmlns:xlink="http://www.w3.org/1999/xlink"添加到您的 SVG 标签中。

编辑

正如我在你发布的图片中看到的那样,你在.svg中声明xmlns:xlink得很好。但是在Chrome渲染的页面中,没有这样的事情!

这是我看到的(Chrome 30(:

<svg height="100%" version="1.1" width="100%" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 165 96" preserveAspectRatio="meet" style="overflow: hidden; position: relative;">

从这里开始,我就知道了:浏览器可以将标签的某些命名空间存储在其他地方吗?我已经搜索了它的 DOM 属性,找不到它。

另一个线索:你也宣布xmlns:svg

引自前一个链接:

绑定时请注意不要键入 xmlns:svg 而不仅仅是 xmlns SVG 命名空间。这是一个容易犯的错误,但可以 打破一切。它不是使 SVG 成为默认命名空间,而是 将其绑定到命名空间前缀"SVG",这几乎可以肯定 不是要在 SVG 文件中执行的操作。符合标准的浏览器 然后将无法识别任何没有 显式命名空间前缀(可能是大多数(如果不是全部((并失败 将文档呈现为 SVG。

关于xlink的其他文档

我感谢所有的帮助和建议。

这个问题的答案最终是我需要使URL的路径绝对的,而不是相对的。

举个例子,JavaScript 中的一行来自:

"AL": {tooltip: 'Alabama', attr: {fill: '#F9B625', href: '#AL'}},

自:

"AL": {tooltip: 'Alabama', attr: {fill: '#F9B625', href: 'http://richcoy.com/locator/index2.html#AL'}},

一旦我为所有 50 个州更改了此设置,单击地图即可正确从 jasonp 提要中提取数据,并将其显示在 Chrome 和 Firefox 的页面上。

var myURL = document.URL; //example http://www.ourdomain.com/thispage.html#IL
var getTheMagicWord = myURL.split("#");
console.log(getTheMagicWord[1]); //logs IL

最新更新