433 Stimmen

Wie entfernt man den Hash aus window.location (URL) mit JavaScript ohne Seitenaktualisierung?

Ich habe URL wie: http://example.com#something wie kann ich die #something ohne die Seite zu aktualisieren?

Ich habe die folgende Lösung versucht:

window.location.hash = '';

Das Rautensymbol wird dabei jedoch nicht entfernt # aus der URL.

0voto

Engineersticity Punkte 141

Auf der Grundlage einer der oben gegebenen Antworten können Sie dies verwenden:

var scrollV, scrollH
var loc = window.location;
scrollV = document.body.scrollTop;
scrollH = document.body.scrollLeft;
if ("pushState" in history) {
    history.pushState("", document.title, loc.pathname + loc.search);

    // Restore the scroll offset
    document.body.scrollTop = scrollV;
    document.body.scrollLeft = scrollH;

} else {
    loc.hash = "";

    // Restore the scroll offset
    document.body.scrollTop = scrollV;
    document.body.scrollLeft = scrollH;
}

-5voto

Devang Bhagdev Punkte 635

Sie können hash durch null ersetzen

var urlWithoutHash = document.location.href.replace(location.hash , "" );

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X