मैंने एक वेब ऐप बनाया है जो इतिहास को अपडेट करने के दौरान पृष्ठों के माध्यम से नेविगेट करने के लिए history pushStateऔर replaceStateतरीकों का उपयोग करता है ।
स्क्रिप्ट स्वयं लगभग पूरी तरह से काम करती है; यह पृष्ठों को सही ढंग से लोड करेगा, और जब उन्हें फेंकने की आवश्यकता होती है तो पृष्ठ त्रुटियों को फेंक देंगे। हालाँकि, मैंने एक विचित्र समस्या पर ध्यान दिया है जहाँ pushStateइतिहास में कई, डुप्लिकेट प्रविष्टियों (और इससे पहले की प्रविष्टियों को प्रतिस्थापित करें) को धकेल दिया जाएगा।
उदाहरण के लिए, मान लें कि मैं निम्नलिखित (क्रम में) करता हूं:
Index.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। मैंने इसका परीक्षण नहीं किया है, केवल जंगली अनुमान लगा रहा हूं, इसलिए इसे टिप्पणी के रूप में जोड़ रहा हूं और उत्तर नहीं।