简体   繁体   中英

Separate Javascript file not Functioning

Good day, I just made a simple javascript file. my only issue is the alert is not popping up. any ideas? any comment is appreciated thank you.

html file:

<html>
    <head>
    <script src="practice2.js"></script>
        <title>Practice</title>
    </head>

    <body>
        <h1>Title</h1>
        <p>sample paragraph</p>

        <ul>
            <li>List 1</li>
            <li>List 2</li>
            <li>List 3</li>
        </ul>


    </body>
</html>

javascipt file:

var year = 2018;

function myFunction() {
    if ( year == 2018 ) 
        {   
        alert("it is equal to 2018");
        }
}

You just define your function needs to call your function like following:

 var year = 2018; function myFunction() { if ( year == 2018 ) { alert("it is equal to 2018"); } } myFunction();// call your function 

Try this:-You missed to call the function

 var year = 2018; function myFunction() { if ( year == 2018 ) { alert("it is equal to 2018"); } } 
 <html> <head> <script src="practice2.js"></script> <title>Practice</title> </head> <body> <h1>Title</h1> <p>sample paragraph</p> <ul> <li>List 1</li> <li>List 2</li> <li>List 3</li> </ul> <input type="submit" value="Call Function" onclick="myFunction()"/> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM