1
- const links = document . querySelectorAll ( 'link[rel="import"]' )
1
+ // copy from https://github.com/electron/electron-api-demos/pull/466/
2
2
3
- // Import and add each page to the DOM
4
- Array . prototype . forEach . call ( links , function ( link ) {
5
- let template = link . import . querySelector ( '.task-template' )
6
- let clone = document . importNode ( template . content , true )
7
- if ( link . href . match ( 'about.html' ) ) {
8
- document . querySelector ( 'body' ) . appendChild ( clone )
9
- } else {
10
- document . querySelector ( '.content' ) . appendChild ( clone )
11
- }
12
- } )
3
+ function includeHTML ( ) {
4
+ var links , i , elmnt , href , request ;
5
+ /* Loop through a collection of all HTML elements: */
6
+ links = document . querySelectorAll ( 'link[rel="import"]' ) ;
7
+ // console.log(links)
8
+ for ( i = 0 ; i < links . length ; i ++ ) {
9
+ elmnt = links [ i ] ;
10
+ /*search for elements with a certain atrribute:*/
11
+ href = elmnt . getAttribute ( 'href' ) ;
12
+ // console.log(href)
13
+ if ( href ) {
14
+ /* Make an HTTP request using the attribute value as the file name: */
15
+ request = new XMLHttpRequest ( ) ;
16
+ request . onreadystatechange = function ( ) {
17
+ if ( this . readyState == 4 ) {
18
+ if ( this . status == 200 ) { elmnt . innerHTML = this . responseText ; }
19
+ if ( this . status == 404 ) { elmnt . innerHTML = "Page not found." ; }
20
+ // console.log(elmnt) // <link ref="import" href="sections/windows/crash-hang.html">
21
+ let template = elmnt . querySelector ( '.task-template' )
22
+ let clone = document . importNode ( template . content , true )
23
+ if ( href . match ( 'about' ) ) {
24
+ document . querySelector ( 'body' ) . appendChild ( clone )
25
+ } else {
26
+ document . querySelector ( '.content' ) . appendChild ( clone )
27
+ }
28
+ elmnt . remove ( ) ;
29
+ includeHTML ( ) ;
30
+ }
31
+ }
32
+ request . open ( "GET" , href , false ) ; // `false` makes the request synchronous
33
+ request . send ( ) ;
34
+ /* Exit the function: */
35
+ return ;
36
+ }
37
+ }
38
+ }
39
+ includeHTML ( ) ;
0 commit comments