【问题标题】:Get the full URL query string from document.location.href?从 document.location.href 获取完整的 URL 查询字符串?
【发布时间】:2018-11-05 14:53:46
【问题描述】:

我需要获取完整的网址

我需要从停在同一个基本域站点的登录页面上的域交叉。

我有一个这样的 URL 示例:登录页面 http://landing.name 跨到基本域 http://domain.name/test.html

我需要手动定义完整的 URL

我的代码:

$('.add-it').click(function(ev) {

        ev.preventDefault();

        var uri = $(this).attr('href');

        $.get( uri, function( r ) {

            swal({
                title: "Test Title!",   
                text: r + "Test",   
                showCancelButton: true,   
                confirmButtonColor: "#DD6B55",   
                confirmButtonText: "Test",   
                cancelButtonText: "Test",   
                closeOnConfirm: true,   
                closeOnCancel: true, 
                html: true
            }, function(isConfirm) {   
                if (isConfirm) {     
                    document.location.href = '/test.html';
                } 
            });

        }).fail(function(xhr, status, error) {
            swal({ title: 'woops', text: error, type: "warning",  }); // or whatever
        });

        return false;

    });

实际代码运行良好,但没有完整的 URL

我试着改变

document.location.href = '/test.html';

location.href = 'http://domain.name/test.html';
window.location.href = 'http://domain.name/test.html';

但不起作用。

请帮忙? 非常感谢。

【问题讨论】:

  • 请指定您要执行的操作,因为设置document.location.href 工作正常。
  • 嗨 Domenik,我需要从停在同一个基本域中的登录页面上的域交叉我有这样的 URL 示例:登录页面 landing.name 交叉到基本域 domain.name/test.html跨度>
  • 在控制台中尝试window.location.href = 'http://domain.name/test.html';,例如。 google.com,它的工作原理。

标签: jquery string url


【解决方案1】:

为了获取当前网址,您可以使用

window.location.href 

document.URL;

要获取主机名,您可以使用window.location.hostname

所以你可以这样做

// 设置当前窗口的新位置。

window.location = window.location.hostname+'/test.html';

// 为当前窗口设置新的href(URL)。

window.location.href = window.location.hostname+'/test.html';

【讨论】:

  • 谢谢 bhunu 但现在可以工作了,我需要从停在同一个基本域站点的登录页面上的域交叉,我有一个这样的 URL 示例:登录页面 landing.name 交叉到基本域 @ 987654322@,我需要手动定义完整的 URL
  • 可以通过window.location.hostname获取域名
  • 是的,但是将域名停在登陆页面landing.name/test.html而不是基本域:(因此我需要制作完整的网址,谢谢好友
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-15
  • 1970-01-01
  • 2011-02-21
  • 1970-01-01
相关资源
最近更新 更多