Вопрос: Получить текущий URL-адрес в JavaScript?


Я использую jQuery. Как получить путь к текущему URL-адресу и присвоить его переменной?

Пример URL:

http://localhost/menuname.de?foo=bar&number=0

1565


источник


Ответы:


Чтобы получить путь, вы можете использовать:

var pathname = window.location.pathname; // Returns path only
var url      = window.location.href;     // Returns full URL

2104



В чистом стиле jQuery:

$(location).attr('href');

Объект location также имеет другие свойства, такие как хост, хэш, протокол и имя пути.


773



http://www.refulz.com:8082/index.php#tab2?foo=789

Property    Result
------------------------------------------
host        www.refulz.com:8082
hostname    www.refulz.com
port        8082
protocol    http:
pathname    index.php
href        http://www.refulz.com:8082/index.php#tab2
hash        #tab2
search      ?foo=789

var x = $(location).attr('<property>');

Это будет работать, только если у вас есть jQuery. Например:

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js">
</script>
  $(location).attr('href');      // http://www.refulz.com:8082/index.php#tab2
  $(location).attr('pathname');  // index.php
</script>
</html>

423



Если вам нужны хеш-параметры, присутствующие в URL-адресе, window.location.hrefможет быть лучшим выбором.

window.location.pathname
=> /search

window.location.href 
 => www.website.com/search#race_type=1

57



Вы захотите использовать встроенный JavaScript window.locationобъект.


45



Just add this function in JavaScript, and it will return the absolute path of the current path.

function getAbsolutePath() {
    var loc = window.location;
    var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
    return loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));
}

I hope it works for you.


36



window.location is an object in javascript. it returns following data

window.location.host          #returns host
window.location.hostname      #returns hostname
window.location.path          #return path
window.location.href          #returns full current url
window.location.port          #returns the port
window.location.protocol      #returns the protocol

in jquery you can use

$(location).attr('host');        #returns host
$(location).attr('hostname');    #returns hostname
$(location).attr('path');        #returns path
$(location).attr('href');        #returns href
$(location).attr('port');        #returns port
$(location).attr('protocol');    #returns protocol

29



This is a more complicated issue than many may think. Several browsers support built-in JavaScript location objects and associated parameters/methods accessible through window.location or document.location. However, different flavors of Internet Explorer (6,7) don't support these methods in the same way, (window.location.href? window.location.replace() not supported) so you have to access them differently by writing conditional code all the time to hand-hold Internet Explorer.

So, if you have jQuery available and loaded, you might as well use jQuery (location), as the others mentioned because it resolves these issues. If however, you are doing-for an example-some client-side geolocation redirection via JavaScript (that is, using Google Maps API and location object methods), then you may not want to load the entire jQuery library and write your conditional code that checks every version of Internet Explorer/Firefox/etc.

Internet Explorer makes the front-end coding cat unhappy, but jQuery is a plate of milk.


25



For the host name only, use:

window.location.hostname

24



This will also work:

var currentURL = window.location.href;

19