javascript - Obtenez l'URL actuelle avec jQuery

Mots clés : javascriptjqueryurljavascript

meilleur 4 Réponses javascript - Obtenez l'URL actuelle avec jQuery

vote vote

97

var pathname = window.location.pathname; // Returns path only (/path/example.html) var url      = window.location.href;     // Returns full URL (https://example.com/path/example.html) var origin   = window.location.origin;   // Returns base URL (https://example.com) 
vote vote

90

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

72

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

70

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

Questions similaires