Javascript - 检查哈希值,忽略以下分析代码

发布于 2024-11-29 08:09:36 字数 613 浏览 5 评论 0原文

我们正在将视频添加到网站的主页,并希望每当 URL 中出现 #video 标签时能够自动弹出视频(在灯箱样式的容器中):

http:// www.domain.com#video

如果在网站内部点击链接(即:),则需要弹出视频如果哈希值存在于页面加载时的 URL 中。

使用 window.location.hash 或单击带有哈希的链接并触发关联的 JavaScript 函数时,可以轻松检查 URL 中的哈希。这按预期工作,没有任何问题。

但是,由于此 URL 将在自动添加 Google Analytics 代码的电子邮件中发送,因此分析代码将附加到 URL 的末尾:

http://www.domain.com#video?utm_source=...< /code>

由于分析代码会随每个电子邮件活动而变化,因此我无法执行简单的 Javascript replace() 来忽略分析代码。

如何检查 URL 中是否存在哈希值,但忽略 后面的任何内容?如果存在?

We are adding a video to the home page of a site, and want to be able to automatically pop up the video (in a lightbox-style container) whenever the #video tag is present in the URL:

http://www.domain.com#video

The video needs to pop up if a link is clicked internally on the site (ie: <a href="#video">) and also if the hash is present in the URL on page load.

Easy enough to check for the hash in the URL using window.location.hash or when a link with the hash is clicked and fire the associated javascript function. That's working as expected without any issues.

However as this URL will be sent out in emails with Google Analytics code automatically added, the analytics code is appended to the end of the URL:

http://www.domain.com#video?utm_source=...

Because the analytics code will change with each email campaign, I can't do a simple Javascript replace() to ignore the analytics code.

How do I go about checking whether the hash is present in the URL, but ignore anything after a ? if present?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那小子欠揍 2024-12-06 08:09:36

URL 的正确形式不是在查询参数后面添加哈希值吗?然后,location.hash 变量将正常工作,并且您不需要特殊代码。您的 URL 应如下所示,然后您可以直接使用 location.hash ,即 #video

http://www.domain.com?utm_source=xxx#video

我不建议将此作为解决方案(我认为您应该获取网址固定为合法),但您可以使用此代码来解析URL 之外的哈希值,即使它位于非法位置:

var matches = window.location.href.match(/#(.*?)(\?|$)/);
if (matches) {
    var hash = matches[1];
}

此代码从“#”提取到字符串的任一末尾或“?”以先到者为准。您可以在此处看到它在一堆测试 URL 上运行: http://jsfiddle.net/jfriend00/HuqL7/< /a>.

Isn't the proper form of a URL to have the hash at the end after the query parameters? Then, the location.hash variable will work properly and you won't need special code. Your URL should look like this and then you can just directly use location.hash which will be #video:

http://www.domain.com?utm_source=xxx#video

I don't advise this as the solution (I think you ought to get the URLs fixed to be legal), but you can use this code to parse the hash value out of the URL even if it's in the illegal position:

var matches = window.location.href.match(/#(.*?)(\?|$)/);
if (matches) {
    var hash = matches[1];
}

This code extracts from the "#" to either end of string or "?" whichever comes first. You can see it run on a bunch of test URLs here: http://jsfiddle.net/jfriend00/HuqL7/.

乙白 2024-12-06 08:09:36
location.hash.match(/[^?]*/)

假设哈希值总是第一个,那就应该可以了。

location.hash.match(/[^?]*/)

Assuming the hash is always first, that should do it.

女中豪杰 2024-12-06 08:09:36

(这是对您问题的字面答案,但有一个巨大的警告)从技术上讲,您可以测试:

var h = window.location.hash;
var ind = h.indexOf( '?' );
var test = ind <0?h:h.substr(0, ind)

如果您希望 Google Analytics 正常工作,您可能会遇到问题#。规则是 # 之后的所有内容都不会发送到服务器,这意味着您的 Analytics 可能会消失。您需要确保您的哈希值添加在所有 Google 内容之后。如果是的话,那么您就无需担心测试任何内容。

(This is a literal answer to your question, but there is a huge caveate) Technically, you can just test:

var h = window.location.hash;
var ind = h.indexOf( '?' );
var test = ind <0?h:h.substr(0, ind)

If you want Google Analytics to work, you may have problems #. The rule is that everything after a # is not sent to the server, which means that your Analytics may go out the window. You need to make sure that your hash is added after all of the Google stuff. If it is, then you won't need to worry about testing anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文