How to call external JavaScript function in HTML

前端 未结 2 1818
悲&欢浪女
悲&欢浪女 2020-12-02 23:14

I have a small chunk of code I can\'t seem to get working. I am building a website and using JavaScript for the first time. I have my JavaScript code in an external file \'M

相关标签:
2条回答
  • 2020-12-02 23:36

    If a <script> has a src then the text content of the element will be not be executed as JS (although it will appear in the DOM).

    You need to use multiple script elements.

    1. a <script> to load the external script
    2. a <script> to hold your inline code (with the call to the function in the external script)

      scroll_messages();
    0 讨论(0)
  • 2020-12-02 23:45

    In Layman terms, you need to include external js file in your HTML file & thereafter you could directly call your JS method written in an external js file from HTML page. Follow the code snippet for insight:-

    caller.html

    <script type="text/javascript" src="external.js"></script>
    <input type="button" onclick="letMeCallYou()" value="run external javascript">
    

    external.js

    function letMeCallYou()
    {
        alert("Bazinga!!!  you called letMeCallYou")
    }
    

    Result :

    0 讨论(0)
提交回复
热议问题