【问题标题】:Replace URL in address bar with a subdomain withOUT redirecting将地址栏中的 URL 替换为不重定向的子域
【发布时间】:2016-09-23 22:28:25
【问题描述】:

我可以完全控制我的整个域及其子域,我想用子域替换地址栏中显示的 URL,但 重定向页面。

例如,当我加载subdomain.mydomain.com?register 时,我希望地址栏显示 register.mydomain.com 而不重定向。

mydomain.com 的所有页面和子域 已经在其外部 .js 文件的 $(window).load(function () { 代码中将 document.domain = 'mydomain.com'; 作为 first 事物.

我尝试history.pushState("", "", '/'); 去除subdomain.mydomain.com 之后的任何内容(有效),并且我已经能够在/ 之后显示一些东西(这也有效) ,但我无法让显示的子域更改之前 /

我该怎么做?有可能吗?如果是这样,它是否依赖于document.domain?如果是这样,我是否正确执行document.domain??


我应该放吗

 <script type="text/javascript">
 document.domain = 'mydomain.com';
 </script>

本身在我的 HTML 文件的 &lt;head&gt; 中,还是在其外部 .js 文件的 $(window).load(function () { 代码中正常工作?

【问题讨论】:

    标签: javascript jquery html cross-domain subdomain


    【解决方案1】:

    我不确定域/子域的内容,但我有一个页面,用户可以在其中单击多个项目,并根据他/她的输入,在浏览器的地址栏中显示一个 URL变化。我可以在不重定向的情况下做到这一点:

    var newurl = window.location.protocol 
        + "//" + window.location.host + window.location.pathname 
        + StrippedQueryString + '&H=' + Parameter;
    
    if (history.replaceState) {
        window.history.replaceState({ path: newurl }, '', newurl);
    }
    

    希望这会有所帮助。

    【讨论】:

    • 我最终选择了pushState(); 而不是replaceState();,这样后退按钮仍然可以按照我需要的方式工作。不过非常感谢!!这本来可以满足我的需要,但服装不允许后退按钮正常工作。来源:http://stackoverflow.com/a/17507140/3762525
    猜你喜欢
    • 2019-08-23
    • 2013-09-20
    • 2013-05-21
    • 2012-07-11
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    相关资源
    最近更新 更多