Ik gebruik jQuery. Hoe krijg ik het pad van de huidige URL en wijs ik het toe aan een variabele?
Voorbeeld-URL:
http://localhost/menuname.de?foo=bar&number=0
Antwoord 1, autoriteit 100%
Om het pad te krijgen, kunt u het volgende gebruiken:
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)
Antwoord 2, autoriteit 31%
In pure jQuery-stijl:
$(location).attr('href');
Het locatie-object heeft ook andere eigenschappen, zoals host, hash, protocol en padnaam.
Antwoord 3, autoriteit 18%
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>');
Dit werkt alleen als je jQuery hebt. Bijvoorbeeld:
<html>
<script src="https://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>
Antwoord 4, autoriteit 3%
Als je de hash-parameters in de URL nodig hebt, is window.location.href
wellicht een betere keuze.
window.location.pathname
=> /search
window.location.href
=> www.website.com/search#race_type=1
Antwoord 5, autoriteit 2%
U wilt de ingebouwde window.location
object.
Antwoord 6, autoriteit 2%
Voeg deze functie gewoon toe aan JavaScript, en het zal het absolute pad van het huidige pad teruggeven.
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));
}
Ik hoop dat het voor u werkt.
Antwoord 7
Dit werkt ook:
window.location.hostname
8
Hieronder volgen voorbeelden van handige codefragets die kunnen worden gebruikt – sommige van de voorbeelden gebruiken standaard JavaScript-functies en zijn niet specifiek voor jQuery:
Zie 8 nuttige jQuery-fragmenten voor URL’s & amp; Querystrings .
9
Hier is een voorbeeld om de huidige URL te krijgen met JQuery en JavaScript:
$(document).ready(function() {
//jQuery
$(location).attr('href');
//Pure JavaScript
var pathname = window.location.pathname;
// To show it in an alert window
alert(window.location);
});
$.getJSON("idcheck.php?callback=?", { url:$(location).attr('href')}, function(json){
//alert(json.message);
});
10
Alle browsers ondersteunen Javascript Window Object. Het definieert het venster van de browser.
De globale objecten en functies worden automatisch onderdeel van het vensterobject.
Alle globale variabelen zijn vensterobjecten en alle wereldwijde functies zijn de methoden.
Het hele HTML-document is ook een venster-eigenschap.
, zodat u Window.location-object kunt gebruiken om alle URL-gerelateerde kenmerken te krijgen.
javascript
console.log(window.location.host); //returns host
console.log(window.location.hostname); //returns hostname
console.log(window.location.pathname); //return path
console.log(window.location.href); //returns full current url
console.log(window.location.port); //returns the port
console.log(window.location.protocol) //returns the protocol
11
Window.locatie geeft u de huidige URL , en u kunt extraheren wat u wilt …
12
Gebruik dit als u het pad van de hoofdsite wilt ontvangen:
$(location).attr('href').replace($(location).attr('pathname'),'');
13
Zie purl.js . Dit zal echt helpen en kan ook worden gebruikt, afhankelijk van jQuery. Gebruik het als volgt:
$.url().param("yourparam");
14
var path = location.pathname
Retourneert het pad van de huidige URL (JQuery is niet nodig). Het gebruik van window.location
is optioneel.
15
Zeer vaak gebruikte top 3 zijn
1. window.location.hostname
2. window.location.href
3. window.location.pathname
Antwoord 16
var newURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
Antwoord 17
// get current URL
$(location).attr('href');
var pathname = window.location.pathname;
alert(window.location);
Antwoord 18
In jstl hebben we toegang tot het huidige URL-pad met pageContext.request.contextPath
. Als je een ajax-oproep wilt doen,
url = "${pageContext.request.contextPath}" + "/controller/path"
Bijvoorbeeld: op de pagina http://stackoverflow.com/questions/406192
geeft dit http://stackoverflow.com/controller/path