0

I'm looking to replace document.write() in this code but can't seem to figure out the best alternative. I'm trying to optimize my site and document.write() is causing some optimization problems. Thanks in advance! You can check it out live here Gaming Forum

Here's my code:

    initAutodetect: function() {
    if (!thstyleswitchConfig.autodetectEnabled) {
        return;
    }
    if (themehouse.styleSwitch.autodetectInitialized) {
        return;
    }
    themehouse.styleSwitch.autodetectInitialized = true;
    var preferred = themehouse.styleSwitch.getPreferredStyleType();

    var write = '';
    if (preferred === null) {
        var defaultStyleKey = thstyleswitchConfig.currentStyleType + 'Style';
        var styleOpts = thstyleswitchConfig[defaultStyleKey];

        write = themehouse.styleSwitch.buildStyleLinkTag(styleOpts.primaryCssUrl) + themehouse.styleSwitch.buildStyleLinkTag(styleOpts.additionalCssUrl);
    } else {
        write = themehouse.styleSwitch.buildStyleLinkTag(thstyleswitchConfig.lightStyle.primaryCssUrl, 'light') +
            themehouse.styleSwitch.buildStyleLinkTag(thstyleswitchConfig.lightStyle.additionalCssUrl, 'light') +
            themehouse.styleSwitch.buildStyleLinkTag(thstyleswitchConfig.darkStyle.primaryCssUrl, 'dark') +
            themehouse.styleSwitch.buildStyleLinkTag(thstyleswitchConfig.darkStyle.additionalCssUrl, 'dark');
    }

    document.write(write);
},
JoyFreak
  • 1
  • 2
  • 1
    `document.body.innerHTML = ;` is an alternative – Taplar Dec 07 '20 at 22:59
  • Be carefull with `innerHTML`, it also parses ` – Countour-Integral Dec 07 '20 at 23:02
  • That doesn't work for me :(. I replaced "document.write(write);" with "document.body.innerHTML = write;" and that didn't work. – JoyFreak Dec 07 '20 at 23:04
  • Define "optimize" and "optimization problems" and you've at least got a bit of a question. Right now, who knows what the desired result is... – spender Dec 07 '20 at 23:04
  • @Countour-Integral ` – ControlAltDel Dec 07 '20 at 23:05
  • Here, tells me to avoid using document.write() https://gtmetrix.com/reports/www.joyfreak.com/8Tb6v3VA/ – JoyFreak Dec 07 '20 at 23:07
  • Did any of you actually look at my use case? The above alternatives don't work. I've provided a code above of how the document.write is being used. – JoyFreak Dec 07 '20 at 23:08
  • 1
    Are you suggesting that we can take the code you have provided and make a runnable example of your issue? I highly disagree. – Taplar Dec 07 '20 at 23:10
  • No need to do that. – JoyFreak Dec 07 '20 at 23:11
  • @JoyFreak That "recommendation" from gtmetrix does not apply to you, since what you are doing, is adding **styles** and not **scripts**. "For users on slow connections, *external scripts dynamically injected via `document.write()`* can delay page load by tens of seconds.". If you want to know more on why that would be a problem, take a look into this series of [Critical Rendering Path](https://developers.google.com/web/fundamentals/performance/critical-rendering-path). – David Fontes Dec 07 '20 at 23:27
  • Yes but it's warning me to avoid it and for that, my score is low. – JoyFreak Dec 07 '20 at 23:45
  • If that is an issue for you, then you can use DOM manipulation. Search for the element where you want to add those styles (probably in the head) and append to it. If you need some help on that, update your question to include your HTML code (the relevant parts), where your script is executed (the script tag) and where do you want to place the style tags. – David Fontes Dec 07 '20 at 23:51

0 Answers0