Changeset 38912 for documented-examples


Ignore:
Timestamp:
2024-04-09T12:56:44+12:00 (3 weeks ago)
Author:
kjdon
Message:

indented this nicely. Also, we need to check whether linkEl is null - which it might be if user had previously clicked 'none'. In that case lets just rload the current page, which gets us back with all stylesheet links restored. to the default, not the one the user clicked, but never mind...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • documented-examples/trunk/style-e/script/custom-script.js

    r36371 r38912  
    11function replaceStyle(stylesheet) {
    2     //alert("In custom-sript.js::ReplaceStyle()");
    3     var body = document.getElementsByTagName('body')[0];
    4     var linkEl = document.getElementById("custom-style");
    5     var href = linkEl.getAttribute("href");
    6     var lastSlashIndex = href.lastIndexOf("/");
    7     var hrefPrefix = href.substring(0, lastSlashIndex);
     2//  alert("In custom-sript.js::ReplaceStyle()");
     3    var body = document.getElementsByTagName('body')[0];
     4    var linkEl = document.getElementById("custom-style");
     5    if (linkEl === null) {
     6    // we had previously clicked null, which means all stylesheet links are removed
     7    // so we can't update a link. Lets just reload the page. It won't then use the selected stylesheet, but never mind.
     8    location.reload();
     9    return;
    810
    9     href = hrefPrefix + "/" + stylesheet + ".css";
    10     linkEl.setAttribute("href", href);
    11 
     11    }
     12    var href = linkEl.getAttribute("href");
     13    var lastSlashIndex = href.lastIndexOf("/");
     14    var hrefPrefix = href.substring(0, lastSlashIndex);
     15   
     16    href = hrefPrefix + "/" + stylesheet + ".css";
     17    linkEl.setAttribute("href", href);
     18   
     19   
     20    // https://techstacker.com/how-to-remove-all-css-from-site-with-javascript/
     21    if(stylesheet == "") { /* gs3-style-red */
     22    document
     23        .querySelectorAll('style,link[rel="stylesheet"]')
     24        .forEach(item => item.remove())
    1225   
    13     // https://techstacker.com/how-to-remove-all-css-from-site-with-javascript/
    14     if(stylesheet == "") { /* gs3-style-red */
    15         document
    16         .querySelectorAll('style,link[rel="stylesheet"]')
    17         .forEach(item => item.remove())
    18        
    19         // https://code-boxx.com/dynamically-load-replace-css/
    20         //document.head.appendChild(linkEl);
    21     }   
    22  
    23     //reloadCss(); // https://stackoverflow.com/questions/2024486/is-there-an-easy-way-to-reload-css-without-reloading-the-page
     26    // https://code-boxx.com/dynamically-load-replace-css/
     27    //document.head.appendChild(linkEl);
     28    }   
     29   
     30    //reloadCss(); // https://stackoverflow.com/questions/2024486/is-there-an-easy-way-to-reload-css-without-reloading-the-page
    2431}
    2532
     
    2835// It changes the current stylesheet to the named one of parameter
    2936function SetStyle(stylename) {
    30     //alert("In custom-sript.js::SetStyle()");
    31   var body = document.getElementsByTagName('body')[0];
    32   body.style.backgroundImage = "";
    33  
    34   styles = document.styleSheets;
    35   var i;
    36   for (i=0;i<styles.length;i++) {
    37     style = styles[i];
    38     if (style.title == stylename)
    39       style.disabled = false;
    40     else
    41       style.disabled = true;
    42   }
     37    //alert("In custom-sript.js::SetStyle()");
     38    var body = document.getElementsByTagName('body')[0];
     39    body.style.backgroundImage = "";
     40   
     41    styles = document.styleSheets;
     42    var i;
     43    for (i=0;i<styles.length;i++) {
     44    style = styles[i];
     45    if (style.title == stylename)
     46        style.disabled = false;
     47    else
     48        style.disabled = true;
     49    }
    4350}
    4451
Note: See TracChangeset for help on using the changeset viewer.