我如何修改我的JS代码与我的新URL结构的工作
How do I modify my JS code to work with my new URL structure?
我正在尝试从URL中提取搜索字符串。
当URL看起来像这样时,我可以这样做:
http://domain.com/search/keyword
,而不是当URL看起来像这样:
http://domain.com/#/search/keyword
下面是我的第一个例子的Javascript代码,除了我不能让它工作,现在的URL结构已经改变。
function () {
// find and store the path of the current page
var pagePath = location.pathname;
// if the path starts with /search/, we know it's an internal SERP
if (pagePath.indexOf("/search/") === 0) {
// if the above checks out, let's figure out where the search term begins in the URL. Then, let's use that starting point to pull out the search term itself.
var searchString = pagePath.substring(pagePath.lastIndexOf("/") + 1);
// return what we found to GTM as the value of our variable
return searchString;
}
// if it's not an internal SERP page, return "none"
else {
return "none";
}
}
当URL结构看起来像这样时,上面的代码可以正常工作:
http://domain.com/search/keyword 而不是像这样:
http://domain.com/#/search/keyword
问题是:我如何修改我的JS代码上面的工作与我的新URL结构?
我已经尝试改变我的JS代码的一部分:
if (pagePath.indexOf("/search/") === 0) {
if (pagePath.indexOf("/#/search/") === 0) {
但是我不能使它工作。请帮忙:)
散列符号#
表示url片段,不属于pathname
。您可以使用location.hash
相关文章:
- 表单提交问题,如何在我的URL末尾获得ID的值
- 我的URL重定向安全吗
- 为什么我的URL在JavaScript中被调用为弹出窗口
- jQuery 拖放更改我的 URL - 返回 false 不起作用
- 为什么当页面加载 AJAX 时我的 URL 之间没有空格
- 随机的“#”.当我使用 AddThis 代码时,XXXXXXXXXXX 的字符串被添加到我的 URL 中
- 在我的 URL 中添加语言代码
- 如何在使用 django 的 html 页面中从一个选项卡移动到另一个选项卡时将请求发布到我的 url,该页面有多个选项
- 当链接被点击时,英镑符号会出现在我的URL中
- 如何在不重新加载页面的情况下向我的url添加参数
- 如果尖括号是我的URL中唯一未编码的特殊字符,这本身就很危险吗
- Url.Action将一个&在我的url中,我该如何解决这个问题
- 如何删除或更改.html扩展在我的url使用javascript或jQuery
- 添加一个页面到我的url时,使用jquery ajax
- 如何跟踪一个javascript行为的起源,特别是-添加哈希到我的url
- 是否可以在使用data-rel="back"时为我的url添加其他参数?在jquerymobile
- Joomla网站-有一个表单调用javascript函数-但它也重定向我的url -为什么
- 我的URL正则表达式允许空格但不应该't
- 寻找关于我的url解析脚本(Javascript)的一般反馈
- 如何在不重新加载浏览器的情况下更改我的Url