【问题标题】:Add query string or GUiD to the end of URL on page load using Javascript使用 Javascript 在页面加载时将查询字符串或 GUID 添加到 URL 的末尾
【发布时间】:2013-04-05 15:32:10
【问题描述】:

我找到了一个关于如何使用 Javascript 创建 GUID 的示例,但是我将如何获取当前页面 URL 并使用 jQuery 或 Javascript 将 GUID 附加到它。

  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);

我尝试了 window.location = window.location + '?guid=' + guid,但这只是一遍又一遍地重定向页面,同时将 guid 附加到 URL 的末尾。

我只需要在页面加载时将 GUID 附加到 URL 的末尾。 我该怎么做?

提前致谢

【问题讨论】:

    标签: javascript jquery query-string guid


    【解决方案1】:

    使用正则表达式检查该位置是否已有 guid,如果不存在则重新加载页面。

    var lc = window.location;
    if(!(/\?guid=/.test(lc))) {
        window.location = window.location + '?guid=' + guid
    }
    

    【讨论】:

    • 这正是我所需要的。感谢您的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多