Posted By

nshakin on 07/12/13


Tagged

ajax javascript js


Versions (?)

Ajax File Include


 / Published in: JavaScript
 

URL: http://www.javascriptkit.com/dhtmltutors/ajaxincludes.shtml

Include any external text file within html; HTML, CSS and Javascript all work.

  1. // import.js
  2. function HttpRequest(url){
  3. var pageRequest = false //variable to hold ajax object
  4. /*@cc_on
  5. @if (@_jscript_version >= 5)
  6. try {
  7. pageRequest = new ActiveXObject("Msxml2.XMLHTTP")
  8. }
  9. catch (e){
  10. try {
  11. pageRequest = new ActiveXObject("Microsoft.XMLHTTP")
  12. }
  13. catch (e2){
  14. pageRequest = false
  15. }
  16. }
  17. @end
  18. @*/
  19.  
  20. if (!pageRequest && typeof XMLHttpRequest != 'undefined')
  21. pageRequest = new XMLHttpRequest()
  22.  
  23. if (pageRequest){ //if pageRequest is not false
  24. pageRequest.open('GET', url, false) //get page synchronously
  25. pageRequest.send(null)
  26. embedpage(pageRequest)
  27. }
  28. }
  29.  
  30. function embedpage(request){
  31. //if viewing page offline or the document was successfully retrieved online (status code=2000)
  32. if (window.location.href.indexOf("http")==-1 || request.status==200)
  33. document.write(request.responseText)
  34. }
  35.  
  36.  
  37. // Usage
  38. <script type="text/javascript">
  39. HttpRequest("external.htm")
  40. </script>

Report this snippet  

You need to login to post a comment.