반응형
자바 스크립트를 사용하여 리디렉션하지 않고 URL 변경
이 질문에 이미 답변이 있습니다.
이 웹 사이트 http://dekho.com.pk/ads-in-lahore 에서와 같이 리디렉션하지 않고 URL을 변경하는 방법을 알고 싶습니다 . 탭을 클릭하면 URL이 변경되지만 페이지는 완전히 다시로드되지 않습니다. 가능하지 않다는 것을 나타내는 다른 질문이 stackoverflow에 있지만 위에서 언급 한 웹 사이트에서 어떻게 구현했는지 알고 싶습니다. 감사
사용 pushState
:
window.history.pushState("", "", '/newpage');
그들이 무엇을 사용하는지 정확히 알고 싶다면 Backbone.js입니다 ( 4574
및 라인 참조 4981
). 그것은 모두 jQuery 소스와 섞여 있지만 주석Backbone.Router
이 달린 소스 문서 페이지 의 관련 줄입니다 .
지원 검사 :
this._wantsPushState = !!this.options.pushState;
this._hasPushState = !!(this.options.pushState && window.history && window.history.pushState);
route
기능 :
route: function(route, name, callback) {
Backbone.history || (Backbone.history = new History);
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
if (!callback) callback = this[name];
Backbone.history.route(route, _.bind(function(fragment) {
var args = this._extractParameters(route, fragment);
callback && callback.apply(this, args);
this.trigger.apply(this, ['route:' + name].concat(args));
Backbone.history.trigger('route', this, name, args);
}, this));
return this;
},
해시 상태와 푸시 상태 중 선택 :
// Depending on whether we're using pushState or hashes, and whether
// 'onhashchange' is supported, determine how we check the URL state.
if (this._hasPushState) {
Backbone.$(window).bind('popstate', this.checkUrl);
} else if (this._wantsHashChange && ('onhashchange' in window) && !oldIE) {
Backbone.$(window).bind('hashchange', this.checkUrl);
} else if (this._wantsHashChange) {
this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
}
그들이 무엇을하고 있는지에 대한 추가 정보 :
// If we've started off with a route from a `pushState`-enabled browser,
// but we're currently in a browser that doesn't support it...
if (this._wantsHashChange && this._wantsPushState && !this._hasPushState && !atRoot) {
this.fragment = this.getFragment(null, true);
this.location.replace(this.root + this.location.search + '#' + this.fragment);
// Return immediately as browser will do redirect to new url
return true;
// Or if we've started out with a hash-based route, but we're currently
// in a browser where it could be `pushState`-based instead...
} else if (this._wantsPushState && this._hasPushState && atRoot && loc.hash) {
this.fragment = this.getHash().replace(routeStripper, '');
this.history.replaceState({}, document.title, this.root + this.fragment);
}
if (!this.options.silent) return this.loadUrl();
그리고 쿠데타의 은혜 :
// If pushState is available, we use it to set the fragment as a real URL.
if (this._hasPushState) {
this.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, url);
}
맨 위에 제공된 주석이 달린 Backbone.js 링크를 읽어야합니다. 매우 유익합니다.
참고 URL : https://stackoverflow.com/questions/12446317/change-url-without-redirecting-using-javascript
반응형
'IT TIP' 카테고리의 다른 글
PHP : 구분 기호없이 explode와 같이 문자열을 배열로 분할 (0) | 2020.10.28 |
---|---|
Android / Eclipse : res / drawable 폴더에 이미지를 추가하려면 어떻게해야합니까? (0) | 2020.10.28 |
Laravel 5-블레이드 전환 (0) | 2020.10.28 |
Java Date 객체에 대한 Unix epoch 시간 (0) | 2020.10.28 |
xdebug 및 netbeans로 php-cli 스크립트 디버깅? (0) | 2020.10.28 |