在外部js文件中调用脚本内部的html函数

时间:2018-06-18 05:36:15

标签: javascript html node.js

我在html文件的脚本标签内写了一个javascript函数......

<!DOCTYPE html>
<html>    
<head>
    <title> Sample Application </title>    
</head>    
<body>
    <h1 style="text-align:  left">Test</h1>

    <div id="conversation" style="width: 600px; height: 400px; border: 1px solid #ccc; background-color: #eee; padding: 4px; overflow: scroll"></div>
    <form id="chatform" style="margin-top: 10px" onsubmit="return pushChat();">
        <input type="text" id="wisdom" size="80" value="" placeholder="Type your issue">
    </form>
    <script type="text/javascript">
// set the focus to the input box
        document.getElementById("wisdom").focus();

        function pushChat() {

            // if there is text to be sent...
            var wisdomText = document.getElementById('wisdom');
            if (wisdomText && wisdomText.value && wisdomText.value.trim().length > 0) {

                // disable input to show we're sending it
                var wisdom = wisdomText.value.trim();
                wisdomText.value = '';
                wisdomText.locked = false;

                showRequest(wisdom);                
                // send it to the Lex runtime
                botaction(wisdom);
            }
            // we always cancel form submission
            return false;
        }                   

        function botaction(action){

        console.log("action: " + JSON.stringify(action));

        switch (action.intentName) {
        case "details":
            var Id      =   action.userid;
            var arguments   = [Id];
            verify(arguments);
            break;


        default:
            console.log('No action  found.');
            console.log('executing the default action based on response');
            break;
            }           
        }
        function verify(arguments){


        }                       
    </script>    
</body>
</html>

我需要将函数verify(arguments)移动到外部js file.i必须移动它,因为我正在调用一个nodejs子进程,它需要包含一个模块。 如何将函数移动到外部js文件,然后从html文件调用验证函数。

2 个答案:

答案 0 :(得分:0)

制作三个文件。它将解决您的问题。

的index.html

<!DOCTYPE html>
<html>    
<head>
    <title> Sample Application </title>
    <!-- insert these two lines -->
    <script type="text/javascript" src="verify.js" ></script> 
    <script type="text/javascript" src="filename.js" ></script>
</head>    
<body>
     <h1 style="text-align:  left">Test</h1>
    <div id="conversation" style="width: 600px; height: 400px; border: 1px solid #ccc; background-color: #eee; padding: 4px; overflow: scroll">
    </div>
    <form id="chatform" style="margin-top: 10px" onsubmit="return pushChat();">
       <input type="text" id="wisdom" size="80" value="" placeholder="Type your issue">
    </form>  
</body>
</html>

verify.js

 function verify() {

 }

other.js

document.getElementById("wisdom").focus();

    function pushChat() {

        // if there is text to be sent...
        var wisdomText = document.getElementById('wisdom');
        if (wisdomText && wisdomText.value && wisdomText.value.trim().length > 0) {

            // disable input to show we're sending it
            var wisdom = wisdomText.value.trim();
            wisdomText.value = '';
            wisdomText.locked = false;

            showRequest(wisdom);                
            // send it to the Lex runtime
            botaction(wisdom);
        }
        // we always cancel form submission
        return false;
    }                   

    function botaction(action){

    console.log("action: " + JSON.stringify(action));

    switch (action.intentName) {
    case "details":
        var Id      =   action.userid;
        var arguments   = [Id];
        verify(arguments);
        break;


    default:
        console.log('No action  found.');
        console.log('executing the default action based on response');
        break;
        }           
    }

答案 1 :(得分:-1)

您只需将该功能复制粘贴到.js文件(例如:verifys.js),然后使用

将.js文件包含在HTML中。