Jquery/Javascript URL检索不起作用


Jquery/Javascript Retrieval of URL Not Working

我正在尝试获取这个完整的url:https://mail.google.com/mail/u/0/?ui=2#inbox/13047asdee8be4e1

通过使用:

var url = document.location.toString();

但它只返回到https://mail.google.com/mail/u/0/?ui=2

我也尝试过其他方式,比如document.url、window.location,但它仍然能吐出同样的东西。

var url = document.location.toString() + '#' + window.location.hash;

document.location.href似乎也能正常工作,并在我的测试中返回了整个地址栏。

尝试window.location.hash获取#之后的零件。

然后你可以用window.location + '#' + window.location.hash 获得整个东西

您还可以使用window.location.href 获取整个字符串

尝试window.location.href。阅读此:window.location-MDC

请在控制台中运行以确保。

function showLoc()
{
  var x = window.location;
  var t = ['Property - Typeof - Value',
            'window.location - ' + (typeof x) + ' - ' + x ];
  for (var prop in x){
    t.push(prop + ' - ' + (typeof x[prop]) + ' - ' +  (x[prop] || 'n/a'));
  }
  alert(t.join(''n'));
}
showLoc();

免责声明:这是我第一次看到所有其他答案都错了,除了Jason:)这么多好答案。两者都描述了如何以全面的方式解决问题,然后作为事后考虑显示了正确的方法:)

var url = window.location.href;应该这样做。

您不需要任何jQuery。你只需要将散列附加在你的url的和处。这样的东西应该行得通。

var url = document.location.toString() + "#" + window.location.hash;