programing

윈도우의 URL 해시를 다른 응답으로 대체하려면 어떻게 해야 합니까?

telebox 2023. 9. 14. 22:30
반응형

윈도우의 URL 해시를 다른 응답으로 대체하려면 어떻게 해야 합니까?

교체 방법으로 해시 URL(document.location.hash)을 변경하려고 하는데 작동하지 않습니다.

$(function(){
 var anchor = document.location.hash;
 //this returns me a string value like '#categories'

  $('span').click(function(){

     $(window).attr('url').replace(anchor,'#food');
     //try to change current url.hash '#categories'
     //with another string, I stucked here.
  });

});

페이지 변경/새로 고침을 원하지 않고 응답 없이 URL만 교체하고 싶습니다.

참고: href="#food" 솔루션으로 이 문제를 해결하고 싶지 않습니다.

둘중 하나의 용도location아니면window.location대신에document.location후자는 비표준이기 때문에

window.location.hash = '#food';

이렇게 하면 URL의 해시가 사용자가 설정한 값으로 바뀝니다.

언급

당신은 이 질문의 답을 선호할 수 있습니다.

다른 점은.history.replaceState()닻으로 스크롤하는 것이 아니라 네비게이션 바에서만 교체할 수 있습니다.모든 주요 브라우저에서 지원됩니다.

history.replaceState(undefined, undefined, "#hash_value")

언급URL : https://stackoverflow.com/questions/11588482/how-can-i-replace-a-windows-url-hash-with-another-response

반응형