Advertisement
Guest User

Untitled

a guest
Jul 18th, 2014
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  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. return new DOMParser().parseFromString(HTMLString, "text/html");
  33. };
  34.  
  35.  
  36.  
  37. var ZachSite = function(elementContent){
  38. var THIS = this;
  39. THIS.elementContent = elementContent;
  40. window.addEventListener("popstate", function(ev){
  41. THIS.loadSitePage(location.href);
  42. });
  43. };
  44.  
  45. ZachSite.prototype.registerLink = function(elmAnchor){
  46. var THIS = this;
  47.  
  48. if(!elmAnchor instanceof HTMLAnchorElement)
  49. throw new TypeError("Only HTMLAnchorElements");
  50.  
  51. elmAnchor.addEventListener("click", function(ev){
  52. ev.preventDefault();
  53. history.pushState({zachSiteURLRequest: true}, this.textContent, this.href);
  54. THIS.loadSitePage(this.href);
  55. });
  56. };
  57.  
  58. ZachSite.prototype.loadSitePage = function(URL)
  59. {
  60. var THIS = this;
  61. var onSuccess = function(responseData)
  62. {
  63. var newDocument = WebTools.parseDocument(responseData);
  64. var newContent = newDocument.getElementById("content");
  65. // removes old #content contents
  66. while(THIS.elementContent.firstChild)
  67. THIS.elementContent.removeChild(THIS.elementContent.firstChild);
  68. // appends new #content contents
  69. while(newContent.firstChild)
  70. THIS.elementContent.appendChild(newContent.firstChild);
  71. // updates the title
  72. document.title = newDocument.title;
  73. };
  74.  
  75. var onFail = function(httpstatus, data){
  76. alert("Page request returned an error: " + httpstatus + "\n " + data);
  77. };
  78.  
  79. WebTools.requestURL("GET", URL, null, onSuccess, onFail);
  80. document.title = "@@@@@ LOADING!!! @@@@@"; // you can show a loading gif here (and hide it inside onSuccess|onFail())
  81. };
  82.  
  83.  
  84. var site = new ZachSite(document.getElementById("content"));
  85. var siteLinks = document.getElementsByTagName("a");
  86. for(var x=0; x<siteLinks.length; x++)
  87. site.registerLink(siteLinks[x]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement