I am using Netbean IDE 8.1 to build the website.
I build a very simple website with simple Javascript function, window.alert().
Upon my testing, I put the button triggered function in the javascript but the function is not triggering.
If I put the function under the HTML, it does triggered the function.
Any idea?

Please note that I also have linked my HTML file to the Javascript file.

Recommended Answers

All 3 Replies

If you trigger the function in the <head> before the HTML is rendered, then it won't trigger because your button is not there yet. That's why it does work if you put the JS before the closing </body> tag at the end of your HTML, If you want to keep it in the head, then you can wrap your JS in between a document ready function.

Not sure if you're using plain JS or the jQuery library, so here's both:
Plain JS: https://www.competa.com/blog/cross-browser-document-ready-with-vanilla-javascript/
JQuery: http://learn.jquery.com/using-jquery-core/document-ready/

Although it might be better in lots of cases to put all your JS before the closing body tag, becaase JS in the head blocks rendering of the HTML which results in slower loading pages.

<script src="../src/java/Controller/ALGAMEBean.js" type="text/javascript"></script>

This line is within the head section.

Ok, so then leave it at the bottom of the HTML instead of between the head.

<!DOCTYPE html>
<html>

    <head>

        <!-- head stuff here -->

    </head>

    <body>

        <!-- HTML stuff here -->

        <!-- JS stuff at the bottom, before closing body tag -->
        <script src="../src/java/Controller/ALGAMEBean.js"></script>

    </body>

</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.