আমি একটি ওয়েব অ্যাপ তৈরি করেছি যা ইতিহাস আপডেট করার সময় পৃষ্ঠাগুলি নেভিগেট করার জন্য পদ্ধতি history pushStateএবং replaceStateপদ্ধতিগুলি ব্যবহার করে ।
লিপি নিজেই প্রায় নিখুঁতভাবে কাজ করে; এটি পৃষ্ঠাগুলি সঠিকভাবে লোড করবে এবং পৃষ্ঠার ত্রুটিগুলি নিক্ষেপ করার দরকার পড়বে। তবে, আমি একটি অদ্ভুত সমস্যা লক্ষ্য করেছি যেখানে pushStateইতিহাসে একাধিক, সদৃশ এন্ট্রিগুলি (এবং তার আগে এন্ট্রিগুলি প্রতিস্থাপন করবে) ঠেলে দেবে।
উদাহরণস্বরূপ, আসুন আমি বলি যে আমি নিম্নলিখিতগুলি (ক্রমে) করছি:
Index.php লোড আপ (ইতিহাস হবে: সূচক)
প্রোফাইল.php এ নেভিগেট করুন (ইতিহাসটি হবে: প্রোফাইল, সূচি)
Search.php এ নেভিগেট করুন (ইতিহাসটি হবে: অনুসন্ধান, অনুসন্ধান, সূচি)
ড্যাশবোর্ড.এফপি নেভিগেট করুন
তারপরে অবশেষে, এটিই আমার ইতিহাসে আসবে (সর্বাধিক অতি প্রাচীনতম অনুসারে):
ড্যাশবোর্ড
ড্যাশবোর্ড
ড্যাশবোর্ড
অনুসন্ধান
সূচি
এটির সাথে সমস্যাটি হ'ল কোনও ব্যবহারকারী যখন ফরোয়ার্ড বা পিছনের বোতামগুলিতে ক্লিক করেন, তারা হয় ভুল পৃষ্ঠায় পুনর্নির্দেশিত হবে, বা একবার ফিরে যেতে একবারে একাধিকবার ক্লিক করতে হবে। এটি, এবং যদি তারা যায় এবং তাদের ইতিহাসটি পরীক্ষা করে তবে তা কোনও অর্থ দেবে না।
আমার এ পর্যন্ত যা আছে:
var Traveller = function(){
this._initialised = false;
this._pageData = null;
this._pageRequest = null;
this._history = [];
this._currentPath = null;
this.abort = function(){
if(this._pageRequest){
this._pageRequest.abort();
}
};
// initialise traveller (call replaceState on load instead of pushState)
return this.init();
};
/*1*/Traveller.prototype.init = function(){
// get full pathname and request the relevant page to load up
this._initialLoadPath = (window.location.pathname + window.location.search);
this.send(this._initialLoadPath);
};
/*2*/Traveller.prototype.send = function(path){
this._currentPath = path.replace(/^\/+|\/+$/g, "");
// abort any running requests to prevent multiple
// pages from being loaded into the DOM
this.abort();
return this._pageRequest = _ajax({
url: path,
dataType: "json",
success: function(response){
// render the page to the dom using the json data returned
// (this part has been skipped in the render method as it
// doesn't involve manipulating the history object at all
window.Traveller.render(response);
}
});
};
/*3*/Traveller.prototype.render = function(data){
this._pageData = data;
this.updateHistory();
};
/*4*/Traveller.prototype.updateHistory = function(){
/* example _pageData would be:
{
"page": {
"title": "This is a title",
"styles": [ "stylea.css", "styleb.css" ],
"scripts": [ "scripta.js", "scriptb.js" ]
}
}
*/
var state = this._pageData;
if(!this._initialised){
window.history.replaceState(state, state.title, "/" + this._currentPath);
this._initialised = true;
} else {
window.history.pushState(state, state.title, "/" + this._currentPath);
}
document.title = state.title;
};
Traveller.prototype.redirect = function(href){
this.send(href);
};
// initialise traveller
window.Traveller = new Traveller();
document.addEventListener("click", function(event){
if(event.target.tagName === "a"){
var link = event.target;
if(link.target !== "_blank" && link.href !== "#"){
event.preventDefault();
// example link would be /profile.php
window.Traveller.redirect(link.href);
}
}
});
সমস্ত সাহায্য প্রশংসা করা হয়,
চিয়ার্স।
updateHistoryফাংশনে ব্রাউজারের ইতিহাসের জিনিসগুলি যুক্ত করছেন । updateHistoryআপনি যখন ট্র্যাভেলারটিকে ( window.Traveller = new Traveller();, constructor-> init-> send-> render-> -> updateHistory) আরম্ভ করছেন তখন এখন, দু'বার কল করা যেতে পারে , তারপরেও ইভেন্টলিস্টনার redirectথেকে click। আমি এটি পরীক্ষা করেছি না, কেবল বুনো অনুমান করা, সুতরাং এটি একটি মন্তব্য হিসাবে যুক্ত করা এবং উত্তর নয়।