Javascript: Load script when url is localhost

I need load file when url is localhost.

I expect:

if (location.hostname === "localhost" || location.hostname === "127.0.0.1"){
 <script src="script-local.js"></script>
} else { //
 <script src="https://example.com/script.js"></script>
}

It’s posible?

Answer

I think something like this could work:

<html>
    <body>
        <script>
            var script = document.createElement("script");

            if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
                script.src = "http://somedomain.com/somescript.js";
            } else {
                script.src = "http://somedomain.com/someotherscript.js";
            }
  
            document.body.appendChild(script);
        </script>
    </body>
</html>