Advertisement
Guest User

Untitled

a guest
Jul 18th, 2014
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WebTools = {};
  2.  
  3. /**
  4.  * @param       String                                                          method
  5.  * @param       String                                                          URL
  6.  * @param       String                                                          postData
  7.  * @param       Function(responseData)                                          onSuccess
  8.  * @param       Function(Integer HTTPStatusCode, String responseData)           onSuccess
  9.  * @return      void
  10.  */
  11. WebTools.requestURL = function(method, URL, postData, onSuccess, onFail)
  12. {
  13.     var XHR = new XMLHttpRequest();
  14.     XHR.open(method, URL, true);
  15.     XHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
  16.     XHR.setRequestHeader("Pragma", "no-cache");
  17.     XHR.setRequestHeader("Cache-Control", "no-cache");
  18.     XHR.onreadystatechange = function(){
  19.         if(XHR.status == 0)
  20.             return onFail(XHR.status);
  21.         if(XHR.readyState == 4)
  22.             if(XHR.status == 200)
  23.                 onSuccess(XHR.responseText);
  24.             else
  25.                 onFail(XHR.status, XHR.responseText);
  26.     };
  27.     XHR.send(postData);
  28. };
  29.    
  30. WebTools.parseDocument = function(HTMLString)
  31. {
  32.     var doc = new DOMParser;
  33.     doc.parseFromString(req.response, 'text/html');
  34.     return doc;
  35. };
  36.  
  37.  
  38.  
  39. var ZachSite = function(elementContent){
  40.     var THIS = this;
  41.     THIS.elementContent = elementContent;
  42.     window.addEventListener("popstate", function(ev){
  43.         if(ev.state.zachSiteURLRequest){
  44.             THIS.loadSitePage(location.href);
  45.         }
  46.     });
  47. };
  48.  
  49. ZachSite.prototype.registerLink = function(elmLink){
  50.     if(!elmAnchor instanceof HTMLAnchorElement)
  51.         throw new TypeError("Only HTMLAnchorElements");
  52.        
  53.     elmLink.addEventListener("click", function(ev){
  54.         if(this.host == location.host && (this.pathname != location.pathname || this.search != location.search)){
  55.             ev.preventDefault();
  56.             history.pushState({zachSiteURLRequest: true}, this.textContent, this.href);
  57.         }
  58.     });
  59. };
  60.    
  61. ZachSite.prototype.loadSitePage = function(URL)
  62. {
  63.     var THIS = this;
  64.    
  65.     var onSuccess = function(responseData)
  66.     {
  67.         var newDocument = WebTools.parseDocument(responseData);
  68.         var newContent = newDocument.getElementById("content");
  69.         // removes old #content contents
  70.         while(THIS.elementContent.children)
  71.             THIS.elementContent.removeChild(THIS.elementContent.children[0]);
  72.         // appends new #content contents
  73.         while(newContent.children)
  74.             THIS.elementContent.appendChild(newContent.children[0]);
  75.         // updates the title
  76.         document.title = newDocument.title;
  77.     };
  78.    
  79.     var onFail = function(httpstatus, data){
  80.         alert("Page request returned an error: " + httpstatus + "\n " + data);
  81.     };
  82.    
  83.     WebTools.requestURL = function("GET", URL, null, onSuccess, onFail);
  84.     document.title = "@@@@@ LOADING!!! @@@@@"; // you can show a loading gif here (and hide it inside onSuccess|onFail())
  85. };
  86.  
  87.  
  88. var site = new ZachSite(document.getElementById("content"));
  89. var siteLinks = document.getElementsByTagName("a");
  90. for(var x=0; x<siteLinks.length; x++)
  91.     site.registerLink(siteLinks[x]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement