4 * General purpose functions for manipulation of page content.
9 * Written by Keith Marshall <keith@users.osdn.me>
10 * Copyright (C) 2020, 2021, MinGW.OSDN Project
13 * Permission is hereby granted, free of charge, to any person obtaining a
14 * copy of this software and associated documentation files (the "Software"),
15 * to deal in the Software without restriction, including without limitation
16 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 * and/or sell copies of the Software, and to permit persons to whom the
18 * Software is furnished to do so, subject to the following conditions:
20 * The above copyright notice, this permission notice, and the following
21 * disclaimer shall be included in all copies or substantial portions of
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER
30 * DEALINGS IN THE SOFTWARE.
33 function set_content( item, value )
34 { /* Replace the existing content, if any, of the HTML element with
35 * id attribute named "item", (if such an element exists), with new
36 * content as specified by "value".
38 var element = document.getElementById( item );
39 if( element ) element.innerHTML = value;
43 function set_page( title, text )
44 { /* Helper function, for use in overlay page scripts, to update
45 * the "title" and "subtitle" fields within the page header; note
46 * that "text" may, and should, use ASCII hyphen-minus where any
47 * hyphen is to be represented; these will be replaced by HTML
48 * non-breaking hyphen entities, on header field assignment.
50 if( title == "title" ) document.title = text;
51 set_content( "page-".concat( title ), text.replace( /-/g, "‑" ));
54 function load_content( container, src )
55 { /* Set the content of the specified HTML "container" element, by
56 * injection of the entire contents of an external file which is
57 * fetched by http server request; (either an http, or an https,
58 * server connection is required; does not work for local files).
60 var request_handler = new XMLHttpRequest();
61 request_handler.onreadystatechange = function()
62 { if( this.readyState == this.DONE )
65 var element = set_content( container, this.responseText );
66 var idx; element = element.getElementsByTagName( "script" );
67 set_content( "e404-missing-page", document.URL );
68 for( idx = 0; idx < element.length; idx++ )
69 { var onload_action = Function( element[idx].innerHTML );
72 if( src.includes("#") )
73 { src = src.substring( src.indexOf("#") + 1, src.length );
74 element = document.getElementById( src );
75 if( element ) element.scrollIntoView();
79 load_content( container, "missing.html" );
82 request_handler.open( "GET", src, true );
83 request_handler.send();
86 function load_page( src )
87 { /* Load page content from the HTML fragment file, as determined
88 * from the specified "src" URL; if no alternative fragment name
89 * is specified, fall back to loading "about.html".
92 const div = "page-content";
93 set_content( div, null );
94 load_content( "header", "header.html" );
95 if( src.includes( ref ) )
96 src = src.substring( src.indexOf( ref ) + ref.length, src.length );
97 else src = "about.html";
98 load_content( div, src );
101 /* $RCSfile$: end of file */