Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何修改JS代码以使用新的URL结构?_Javascript_Url - Fatal编程技术网

Javascript 如何修改JS代码以使用新的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

我正在尝试从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 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