Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WebTools = {};
- /**
- * @param String method
- * @param String URL
- * @param String postData
- * @param Function(responseData) onSuccess
- * @param Function(Integer HTTPStatusCode, String responseData) onSuccess
- * @return void
- */
- WebTools.requestURL = function(method, URL, postData, onSuccess, onFail)
- {
- var XHR = new XMLHttpRequest();
- XHR.open(method, URL, true);
- XHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
- XHR.setRequestHeader("Pragma", "no-cache");
- XHR.setRequestHeader("Cache-Control", "no-cache");
- XHR.onreadystatechange = function(){
- if(XHR.status == 0)
- return onFail(XHR.status);
- if(XHR.readyState == 4)
- if(XHR.status == 200)
- onSuccess(XHR.responseText);
- else
- onFail(XHR.status, XHR.responseText);
- };
- XHR.send(postData);
- };
- WebTools.parseDocument = function(HTMLString)
- {
- var doc = new DOMParser;
- doc.parseFromString(req.response, 'text/html');
- return doc;
- };
- var ZachSite = function(elementContent){
- var THIS = this;
- THIS.elementContent = elementContent;
- window.addEventListener("popstate", function(ev){
- if(ev.state.zachSiteURLRequest){
- THIS.loadSitePage(location.href);
- }
- });
- };
- ZachSite.prototype.registerLink = function(elmLink){
- if(!elmAnchor instanceof HTMLAnchorElement)
- throw new TypeError("Only HTMLAnchorElements");
- elmLink.addEventListener("click", function(ev){
- if(this.host == location.host && (this.pathname != location.pathname || this.search != location.search)){
- ev.preventDefault();
- history.pushState({zachSiteURLRequest: true}, this.textContent, this.href);
- }
- });
- };
- ZachSite.prototype.loadSitePage = function(URL)
- {
- var THIS = this;
- var onSuccess = function(responseData)
- {
- var newDocument = WebTools.parseDocument(responseData);
- var newContent = newDocument.getElementById("content");
- // removes old #content contents
- while(THIS.elementContent.children)
- THIS.elementContent.removeChild(THIS.elementContent.children[0]);
- // appends new #content contents
- while(newContent.children)
- THIS.elementContent.appendChild(newContent.children[0]);
- // updates the title
- document.title = newDocument.title;
- };
- var onFail = function(httpstatus, data){
- alert("Page request returned an error: " + httpstatus + "\n " + data);
- };
- WebTools.requestURL = function("GET", URL, null, onSuccess, onFail);
- document.title = "@@@@@ LOADING!!! @@@@@"; // you can show a loading gif here (and hide it inside onSuccess|onFail())
- };
- var site = new ZachSite(document.getElementById("content"));
- var siteLinks = document.getElementsByTagName("a");
- for(var x=0; x<siteLinks.length; x++)
- site.registerLink(siteLinks[x]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement