Javascript 如何修改JS代码以使用新的URL结构?
我正在尝试从URL提取搜索字符串 我可以在URL如下所示时执行此操作:Javascript 如何修改JS代码以使用新的URL结构?,javascript,url,Javascript,Url,我正在尝试从URL提取搜索字符串 我可以在URL如下所示时执行此操作: http://domain.com/search/keyword 但当URL如下所示时,情况并非如此: http://domain.com/#/search/keyword http://domain.com/#/search/keyword 下面是为我的第一个示例编写的Javascript代码,只是由于URL结构已经更改,我无法让它工作 function () { // find and store the pa
http://domain.com/search/keyword
但当URL如下所示时,情况并非如此:
http://domain.com/#/search/keyword
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
但看起来不是这样的:
http://domain.com/#/search/keyword
http://domain.com/#/search/keyword
问题是:如何修改上面的JS代码以使用新的URL结构
我尝试将JS代码的一部分从以下内容更改为:
if (pagePath.indexOf("/search/") === 0) {
到
但我不能让它工作。请帮助:)散列符号#
表示一个,并且不是路径名的一部分。您可以使用location.hash