-2

I am building a super simple page that converts Fahrenheit to Celsius and visa versa. On this page, I want to load different moving backgrounds I found online that are in external files. For instance, if the user enters "32" or less in the Fahrenheit section, then the background has a snow effect. I am wanting to accomplish this with pure JavaScript; not Node.js or jQuery, etc.

Here's the code I have:

HTML:

<section id="form_div">
    <form id="conversion_form">
         <label>
            Will you be entering the temperature in Farenheit?  Or Celsius?
         </label>
         <input type="radio" name="measurement" id="farenheit" value="farenheit">Farenheit
         <input type="radio" name="measurement" id="celsius" value="celsius">Celcius

         <span id="measurement_f">
             <label>
                 What is the tempurature (in Farenheit)?
             </label>
             <input type="number" name="temp" id="fTemp" maxlength="4">&deg;F
         </span>
         <span id="measurement_c">
             <label>
                What is the tempurature (in Celsius)?
             </label>
             <input type="number" name="temp" id="cTemp" maxlength="4">&deg;C
         </span>
    </form>

    <button id="F">Convert to Farenheit</button>
    <button id="C">Convert to Celsius</button>

    <section id="output"></section>
</section>

JAVASCRIPT:

    document.getElementById('measurement_f').style.display = 'none';
    document.getElementById('measurement_c').style.display = 'none';
    document.getElementById('F').style.display = 'none';
    document.getElementById('C').style.display = 'none';


    document.getElementById('farenheit').onclick = function(){
            document.getElementById('measurement_c').style.display = 'none';
            document.getElementById('F').style.display = 'none';
            document.getElementById('cTemp').value = '';
            document.getElementById('output').innerHTML = '';

            document.getElementById('measurement_f').style.display = 'block';
            document.getElementById('C').style.display = 'block';
    }   

    document.getElementById('celsius').onclick = function(){
        document.getElementById('measurement_f').style.display = 'none';
        document.getElementById('C').style.display = 'none';
        document.getElementById('fTemp').value = '';
        document.getElementById('output').innerHTML = '';

        document.getElementById('measurement_c').style.display = 'block';
        document.getElementById('F').style.display = 'block';

    }   

    document.getElementById('C').onclick = function() {
        var fTempVal = parseInt(document.getElementById('fTemp').value);
        var cTempVal = (fTempVal - 32) * (5 / 9);
        document.getElementById('output').innerHTML = cTempVal.toFixed('0') + '&deg;C';


        if(fTempVal <= '32') {
           //Load <script src="js/snowstorm.js">
        }

        return false;
    }

    document.getElementById('F').onclick = function() {
        var cTempVal = parseFloat(document.getElementById('cTemp').value);
        var fTempVal = (cTempVal * (9 / 5)) + 32;
        document.getElementById('output').innerHTML = fTempVal.toFixed('0') + '&deg;F';

        if(cTempVal <= '0') {
           //Load <script src="js/snowstorm.js">
        }

        return false;
    }
Matt
  • 51
  • 8
  • 2
    Possible duplicate of [How do I include a JavaScript file in another JavaScript file?](https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file) – MrUpsidown Feb 02 '20 at 20:49
  • That link doesn't include anything about an IF statement or an onclick function, nor does it include "importing" the JS file in an IF statement within an onclick function. Thank you for the suggestion though. – Matt Feb 02 '20 at 20:57
  • The fact that you want to load the script in an `if` statement and/or in an `onclick` function makes absolutely no difference at all. – MrUpsidown Feb 02 '20 at 20:59
  • I guess I'm still not understanding how this applies to my question. They are referring to .mjs extensions and Node.js. I just want to do this with pure javascript. – Matt Feb 02 '20 at 21:16
  • 1
    Why do you include irrelevant sections of code on your question? – Gherman Feb 02 '20 at 21:17
  • Which parts are irrelevant? I included the text field so you can see I am requiring a user's input and the buttons so you can see I need this to happen after the user clicks it. I included all of the JS I have so you can see exactly what I have so far and can tell this must happen within an if statement after the button has been clicked and maybe can help tell me where to include whatever suggestion you may have is if it's not obvious, which some suggestions on here aren't always clear. – Matt Feb 02 '20 at 21:21
  • Did you **read** the accepted answer? It shows multiple ways of *loading a javascript file* which is what you asked for. In particular, one section of the answer is named *Dynamic Script Loading* and contains a complete example of how to do that in vanilla Javascript. – MrUpsidown Feb 02 '20 at 21:23
  • In other words, you should replace the `//Load – MrUpsidown Feb 02 '20 at 21:28
  • Okay, you're right. As mentioned in my question, I was using a external file from an online source. When directly adding the – Matt Feb 02 '20 at 21:46
  • I don't see what would/could be *stopping it* except if you're trying to load an online JS file over HTTP and your page is HTTPS or vice-versa. You should watch your Javascript console for errors. That's another question now... – MrUpsidown Feb 02 '20 at 21:53

1 Answers1

1

You could programmatically load the script by creating a script tag with document.createElement

let script = document.createElement('script');
script.onload = function () {
    // Script has loaded so you can do something with it
};
script.src = // where your script comes from ;

document.head.appendChild(script); 
Ross Coundon
  • 647
  • 1
  • 9
  • 20