简体   繁体   English

Javascript:如何从脚本文件中的html页面调用函数

[英]Javascript : how to call a function from an html page that is in a script file

I have two file , one named index.html and an other named index.js I use Node.js with Electron to make a discord bot. 我有两个文件,一个名为index.html ,另一个名为index.js我将Node.jsElectron结合使用来制造一个不和谐的机器人。 My interface is in html due to electron so I have some textbox to update the status of my bot. 由于电子原因,我的界面是html格式,因此我有一些文本框可以更新我的机器人的状态。 I use Discord.js 我使用Discord.js

This is the code that cause some issue index.html : 这是导致出现问题index.html的代码:

<div id="ActivityChanger">
        <select id="ActivityTypeList">
          <option value="none">None</option>
          <option value="PLAYING">Playing</option>
          <option value="WATCHING">Watching</option>
          <option value="LISTENING">Listening To</option>
          <option value="STREAMING">Streaming</option>
        </select>
        <input type="text" name="TEST" size="40" id="ActivityGame" />
        <button name="Update" type="submit" value="update-true" id="UpdateButton" onclick="UpdateActivity()">
        Update
        </button>
        <script scr="index.js"></script>
      </div>

This code normaly send the information of the textbox and dropdown when the Update button is pressed but I don't want to have Javascript in my .html file so I have the function 按下“ Update按钮时,此代码通常会发送文本框和下拉列表的信息,但我不想在.html文件中包含Javascript,因此可以使用

function UpdateActivity(){
    var selectedValue = document.getElementById("ActivityTypeList").value;
    var selectedGame = document.getElementById("ActivityGame").value;
    if((selectedValue !== 'none') && (selectedGame !== null)){
      UserBot.user.setActivity(selectedGame, {type: selectedValue,})
      console.log('Updated Status' + ` to ${selectedValue} ${selectedGame} `)
    }
}

UserBot is my client UserBot是我的客户

But when I press the burron , in the console , its said that UpdateActivity() is not defined 但是当我在控制台中按下burron时,它说未定义UpdateActivity()

If that's the real HTML you've included, it has a typo in it: 如果这是您包含的真实HTML,则其中包含一个错字:

<script scr="index.js"></script>

That should be src , not scr : 那应该是src ,而不是scr

<script src="index.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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