OSDN Git Service

Improve HTTP-404 notification handling.
authorKeith Marshall <keith@users.osdn.me>
Tue, 8 Nov 2022 23:53:47 +0000 (23:53 +0000)
committerKeith Marshall <keith@users.osdn.me>
Tue, 8 Nov 2022 23:53:47 +0000 (23:53 +0000)
* site.js (load_content): Refactor, to define...
(send_e404_notification, e404_subst): ...these internal functions.
(load_content) [this.readyState == this.DONE]: Use them.

site.js

diff --git a/site.js b/site.js
index 58399d3..1e01433 100644 (file)
--- a/site.js
+++ b/site.js
@@ -51,24 +51,32 @@ function set_page( title, text )
   set_content( "page-".concat( title ), text.replace( /-/g, "&#8209;" ));
 }
 
-function load_content( container, src )
+function load_content( container, src, fallback, wanted )
 { /* Set the content of the specified HTML "container" element, by
    * injection of the entire contents of an external file which is
    * fetched by http server request; (either an http, or an https,
    * server connection is required; does not work for local files).
    */
   var request_handler = new XMLHttpRequest();
+  function send_e404_notification( name )
+  { var notification = set_content( "e404-missing-page", name );
+    if( notification ) notification.removeAttribute( "id" );
+  }
+  function e404_subst( name ){ return name ? name : "missing.html"; }
   request_handler.onreadystatechange = function()
   { if( this.readyState == this.DONE )
       switch( this.status )
       { case 200:
          var element = set_content( container, this.responseText );
-         var idx; element = element.getElementsByTagName( "script" );
-         set_content( "e404-missing-page", document.URL );
-         for( idx = 0; idx < element.length; idx++ )
-         { var onload_action = Function( element[idx].innerHTML );
-           onload_action();
+         if( element )
+         { var idx = 0; element = element.getElementsByTagName( "script" );
+           while( element[idx] )
+           { var onload_action = Function( element[idx++].innerHTML );
+             onload_action();
+           }
          }
+         else if( container ) container.innerHTML = this.responseText;
+         send_e404_notification( fallback ? wanted : document.URL );
          if( src.includes("#") )
          { src = src.substring( src.indexOf("#") + 1, src.length );
            element = document.getElementById( src );
@@ -76,7 +84,7 @@ function load_content( container, src )
          }
          break;
        case 404:
-         load_content( container, "missing.html" );
+         load_content( container, e404_subst( fallback ), fallback, src );
       }
   }
   request_handler.open( "GET", src, true );