简体   繁体   中英

Jquery/Javascript Retrieval of URL Not Working

Im trying to get this complete url: https://mail.google.com/mail/u/0/?ui=2#inbox/13047asdee8be4e1

Through the use of:

var url = document.location.toString();

But it only returns up to https://mail.google.com/mail/u/0/?ui=2

I have also tried other ways like document.url, window.location and it still spits out same thing.

Try window.location.hash to get the part after the # .

You can then get the entire thing with window.location + '#' + window.location.hash

You can also get the entire string with window.location.href

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

document.location.href seemed to work as well and returned the entire address bar in my test.

Try window.location.href . Read this: window.location - MDC

Run this in console to be sure.

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

Disclaimer: This is the first time I am seeing all other answers wrong, except Jason:) So many good answers. Both describing how to go about solving it in a round about way and then showing the correct way as an afterthought:)

var url = window.location.href; should do it.

You don't need any jQuery for that. You'd simply need to append the hash at the and of your url. Something like this should work.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM