JAVASCRIPT   25

ajax loadContent

Guest on 15th June 2022 04:49:17 AM

  1. // JavaScript document
  2.  
  3. // This function will load the pages
  4.  
  5. function ajax_loadContent (container_id, url)
  6.  
  7. {
  8.  
  9.     var required_page = false;
  10.  
  11.     if (window.XMLHttpRequest)
  12.  
  13.     {
  14.  
  15.         // If it's Mozilla, Safari etc
  16.  
  17.         required_page = new XMLHttpRequest ();
  18.  
  19.     } else if (window.ActiveXObject)
  20.  
  21.     {
  22.  
  23.         // but if it's IE
  24.  
  25.         try
  26.  
  27.         {
  28.  
  29.             required_page = new ActiveXObject ("Msxml2.XMLHTTP");
  30.  
  31.         }
  32.  
  33.         catch(e)
  34.  
  35.         {
  36.  
  37.             // in case it's an old version
  38.  
  39.             try
  40.  
  41.             {
  42.  
  43.                 required_page = new ActiveXObject ("Microsoft.XMLHTTP");
  44.  
  45.             }
  46.  
  47.             catch(e)
  48.  
  49.             {
  50.  
  51.             }
  52.  
  53.         }
  54.  
  55.     }
  56.  
  57.     else
  58.  
  59.     return false;
  60.  
  61.     page_required.onreadystatechange = function ()
  62.  
  63.     {
  64.  
  65.         // response function
  66.  
  67.         loadpage(page_required, id_container);
  68.  
  69.     }
  70.  
  71.     page_required.open ('GET', url, true); // assign the open and send methods
  72.  
  73.     page_required.send (null);
  74.  
  75. }
  76.  
  77. // everything is correct and the time has come to put the required information
  78.  
  79. // on your site in the xhtml page
  80.  
  81. function loadpage(page_required, id_container)
  82.  
  83. {
  84.  
  85.     if (page_required.readyState == 4 && (page_required.status == 200 || window.location.href.indexOf ("http") == - 1))
  86.  
  87.     document.getElementById (container_id).innerHTML = required_page.responseText;
  88.  
  89. }

Raw Paste


Login or Register to edit or fork this paste. It's free.