Javascript 在2种情况下向URL添加GUID

Javascript 在2种情况下向URL添加GUID,javascript,query-string,guid,Javascript,Query String,Guid,我已经成功地使用Javascript将GUID添加到URL。 这是我目前使用的代码: <script> if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) ||(navigator.userAgent.match(/iPad/i))) { function S4() { return (((1 + Math.rando

我已经成功地使用Javascript将GUID添加到URL。 这是我目前使用的代码:

    <script>  
  if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) ||(navigator.userAgent.match(/iPad/i))) {
       function S4() { 
           return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);  
       }
    // then to call it, plus stitch in '4' in the third group
       guid = (S4() + S4() + "-" + S4() + "-4" + S4().substr(0, 3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase();
    //alert(guid);
      var lc = window.location;
       if (!(/\?guid=/.test(lc))) { 
          window.location = window.location + '?guid=' + guid 
      }
    }//End if iPhone
</script>

if((navigator.userAgent.match(/iPhone/i))| |(navigator.userAgent.match(/iPod/i))| |(navigator.userAgent.match(/iPad/i))){
函数S4(){
返回((1+Math.random())*0x10000)| 0.toString(16)子字符串(1);
}
//然后叫它,再加上第三组中“4”中的stitch
guid=(S4()+S4()+“-”+S4()+“-4”+S4()。substr(0,3)+“-”+S4()+“-”+S4()+S4())。toLowerCase();
//警报(guid);
var lc=窗口位置;
如果(!(/\?guid=/.test(lc)){
window.location=window.location+'?guid='+guid
}
}//结束如果iPhone
问题:

现在,我想测试URL,看看它是否在URL中已经有查询字符串,如果有,则将GUID附加到查询字符串URL的末尾。我该怎么做

假设我的查询字符串URL如下所示:

然后,我想将GUID添加到查询URL的末尾,如下所示:


注意:您可能会问我为什么要检测iPhone/iPod/iPad并向URL添加GUID。这是因为iOS 6.0+和Mobile Safari中有一个记录在案的、疯狂的bug/功能,该功能尚未通过“超级缓存”POST调用修复。因此,添加GUID会强制好的ol'iToys强制进行新的页面查找。我讨厌破解这个,但是我们已经尝试了Pragma=no-cache、Expires=0和cache-Control=no-cache,并且发现这个问题仍然存在。所以是的。

您可能需要以下逻辑:

var current = window.location.search;
var addon = "";
if (current.charAt(0) !== "?") {    // Querystring Doesn't start with "?"
    addon += "?";
} else {    // Querystring does start with "?" (and maybe more)
    addon += current;
}
if (current.indexOf("guid=") < 0) {    // Querystring Doesn't contain "guid="
    if (current.length > 1) {    // Querystring Contains more than "?_"
        addon += "&";
    }
    addon += "guid=" + guid;
    window.location.href = window.location.protocol + "//" + window.location.host + window.location.pathname + addon;
}
var current=window.location.search;
var addon=“”;
如果(current.charAt(0)!=“?”{//Querystring不以“?”开头
插件+=“?”;
}否则{//Querystring确实以“?”开头(可能更多)
插件+=当前;
}
如果(current.indexOf(“guid=)<0){//Querystring不包含“guid=”
如果(current.length>1){//Querystring包含的字符数超过“?”
插件+=“&”;
}
插件+=“guid=”+guid;
window.location.href=window.location.protocol+“/”+window.location.host+window.location.pathname+addon;
}
因此,它将检查查询字符串是否以“?”开头。如果没有,它会添加它

如果查询字符串中有超过1个字符(意味着它不仅仅是“?”…类似于“?”a”),那么它会添加一个“&”

最后,它还添加了“guid=23482934”(任意值)

因此,情况如下:

  • “”-应变成
    ?guid=12355235
  • “?”-应变成
    ?guid=12355235
  • “?asdf=fdsa”-应变为
    ?asdf=fdsa&guid=12355235

那么问题出在哪里?逻辑/代码看起来是正确的。可能使用
window.location.href
而不是
window.location
。您是否希望在不更改页面的情况下更改页面的URL?我的问题是,如果存在GUID和现有查询字符串,我希望添加GUID和现有查询字符串。如果没有其他查询字符串存在,我可以将GUID添加到URL的末尾,但我不知道在有查询字符串的情况下如何添加GUID。我想将GUID附加到现有的查询字符串(如果存在)。@Ian,哦,等等,我刚碰到它。测试正在查看URL,以查看guid变量是否存在。因此,如果URL中没有“guid”变量,它应该自动工作。这是正确的假设吗?我只是回答了我自己的问题吗?我意识到我错过了一些角落案例。我只是补充了一个答案。不确定您是否完全回答了自己的问题,或者我的回答是否真的回答了,但是请告诉我是否有帮助/有效我认为您的回答有点问题,当我这样做时,“&”替换了提供错误链接的查询字符串,因此URL结果如下。而不是不过这真的很接近。我会尝试把代码弄乱一点,但是如果你知道问题所在,请发帖。Thanks@Carlos是的,忘了那个场景,很抱歉。我刚刚更新了我的答案:)让我知道这是否解决了它!这很有魅力。非常感谢你。我真的很感谢你的帮助。guid和URL操作不是我的强项。其他DOM操作,不太擅长,但不擅长URL。再次感谢@卡洛斯很酷,说真的没问题。很抱歉一开始的混乱:)我相信代码可以重新排序(比如
if
语句)或其他什么,以“更好”,但如果它有效,那就太好了:)