如何在 URL 路径的开头插入 text/path

How to insert text/path in beginning of URL Path

我正在尝试找出一种方法,使用 jQuery.

在 URL 的第一个反斜杠后添加特定单词(在本例中为城市名称)

原因是我创建了一个功能,当用户点击 google 地图上的特定位置时,该功能会使用特定城市信息创建 cookie。然后我使用 jQuery 从该 cookie 中获取城市信息。

下面是我希望它如何运行的示例。

(即:https://linktowebsite.com/services/bumpers add the word "orange" in link -> https://linktowebsite.com/orange/services/bumpers

这就是我目前将城市名称添加到 URL 的方式。我意识到这只会将城市名称添加到 URL 的末尾,而不是在 .com

之后的第一个反斜杠之后
jQuery('#menu-main-menu .menu-item a').each(function() {
  var nLink = jQuery(this);
  var locLink = Cookies.get('location');
   var _href = nLink.attr("href"); 
  nLink.attr("href", _href + locLink);
}); 

这是整个片段!

jQuery( document ).ready(function() {
    
    jQuery('.btnlocation').append('it worked');
    
    var CookieSet = Cookies.get('location');
    
    if (CookieSet == null){
 //no cookie
  console.log( 'No Cookies');
 return false;
} else {
 //have cookie
 var ThisLocation = Cookies.get('location');
   jQuery('#results').html('Welcome to Bumper Buddies ' + ThisLocation);
    
    jQuery('#menu-main-menu .menu-item a').each(function() {
  var nLink = jQuery(this);
  var locLink = Cookies.get('location');
   var _href = nLink.attr("href"); 
  nLink.attr("href", _href + locLink);
});
}



});

如有任何帮助,我们将不胜感激!

一些使用数组的字符串操作应该可以解决问题。

根据您的示例,“linktowebsite.com/services/bumpers”是原始 link,我们从 cookie 中获取“橙色”。这是代码。我添加了三行:

jQuery('#menu-main-menu .menu-item a').each(function() {
  var nLink = jQuery(this);
  var locLink = Cookies.get('location');
  var _href = nLink.attr("href")

  var hrefArray = _href.split("/")   // 1
  hrefArray.splice(1, 0, locLink)    // 2
  
  var newHref = hrefArray.join("/")  // 3

  nLink.attr("href", newHref);
});

我对每一行添加了编号。让我们来看看它们。

  1. 在这里,我们使用 split 方法使用 _href 中的正斜杠将 _href 字符串分隔成一个数组。 hrefArray 现在等于: ["linktowebsite.com", "services", "bumpers"]
  2. splice 是一种方便的方法。您可以在 MDN 文档中 read more about it。在本例中,我使用它来将 locLink 插入到数组中的索引 1 中。 现在,hrefArray 应该是这样的:["linktowebsite.com", "orange", "services", "bumpers"]
  3. 最后,我们使用 join 方法将数组的所有元素连接成一个字符串,每个元素由我们选择的分隔符分隔。在这种情况下,我们可以只使用“/”,这样数组的每个元素都像这样连接:"linktowebsite.com/orange/services/bumpers"

这在使用数组的普通 JS 中是可行的。

您需要将 href 字符串拆分为 split...(创建数组)

然后创建一个新数组:

  1. 第一个使用 shift
  2. 的“拆分 href”项目
  3. 来自 cookie 的城市名称
  4. “拆分 href”项的其余部分

然后使用 join

将它们全部连接回一个字符串

编辑

我给你做了一个函数,它考虑了协议(http://https://),如果存在的话。

function insertCity(href, cookieValue){
  let protocol_match = href.match(/http(s)?:\/\//)
  let protocol = protocol_match ? protocol_match[0] : ""
  let href_splitted = href.replace(protocol,"").split("/")
  return protocol + [href_splitted.shift(), cookieValue, ...href_splitted].join("/")
}

// HTTP
let href1 = "http://linktowebsite.com/services/bumpers"

// HTTPS
let href2 = "https://linktowebsite.com/services/bumpers"

// No protocol?
let href3 = "linktowebsite.com/services/bumpers"

// Some cookie value
let cookie = "orange"

// Testing the function
console.log(insertCity(href1, cookie))
console.log(insertCity(href2, cookie))
console.log(insertCity(href3, cookie))


那就是你的用例:
jQuery(document).ready(function () {
  jQuery(".btnlocation").append("it worked");

  var ThisLocation = Cookies.get("location");

  if (ThisLocation == null) {
    //no cookie
    console.log("No Cookies");
    return false;
  } else {
    //have cookie
    jQuery("#results").html("Welcome to Bumper Buddies " + ThisLocation);

    jQuery("#menu-main-menu .menu-item a").each(function () {
      var nLink = jQuery(this);
      nLink.attr("href", insertCity(nLink.attr("href"), ThisLocation));
    });
  }
});