簡體   English   中英

從webapp中的按鈕訪問外部js文件中的function

[英]Access function in external js file from button in webapp

我的 /js 文件夾中有一個文件 dmreboot_service.js。 當我使用節點 /js/dmreboot_service.js 運行此文件時,它成功調用了 Azure 中的直接方法。

我需要能夠從我的 web 應用程序單擊按鈕執行此 function 或文件。

我嘗試使用以下方法將腳本加載到 html 的頭部:

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

我在外部文件中放了一個警報。 如果我把這個警報放在文件的頂部它可以工作,但在底部它會失敗,所以文件的內容沒有加載。

dmreboot_service.js 的內容是:

 'use strict'; var Registry = require('azure-iothub').Registry; var Client = require('azure-iothub').Client; var connectionString ="HostName=XxxxxxxX.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=XxxxxxxxxxxxxxxxxxxxxxxxxxxxxxX="; var registry = Registry.fromConnectionString(connectionString); var client = Client.fromConnectionString(connectionString); var deviceToReboot = 'Runner'; var startRebootDevice = function (twin) { var methodName = "reboot"; var methodParams = { methodName: methodName, payload: null, timeoutInSeconds: 30 }; client.invokeDeviceMethod(deviceToReboot, methodParams, function(err, result) { if (err) { console.error("Direct method error: "+err.message); } else { console.log("Successfully invoked the device to reboot."); } }); }; var queryTwinLastReboot = function() { registry.getTwin(deviceToReboot, function(err, twin){ if (twin.properties.reported.iothubDM.= null) { if (err) { console:error('Could not query twins. ' + err.varructor:name + '. ' + err;message). } else { var lastRebootTime = twin.properties.reported.iothubDM.reboot;lastReboot. console:log('Last reboot time. ' + JSON,stringify(lastRebootTime, null; 2)). } } else console.log('Waiting for device to report last reboot time;'); }); }; startRebootDevice(), setInterval(queryTwinLastReboot; 2000); alert('dmreboot included!');

我還嘗試在我的 html 的頭部創建一個 function ,其中包含 dmreboot_service.js 的全部內容,但是雖然 ZC1C425268E68385D1AB5074C17A94F14 被成功調用,但代碼沒有執行。

這是我需要開始工作的項目的最后一部分。 我對此很陌生,這讓我發瘋了。! 非常感謝任何建議。

我通常使用 javascript 來處理 HTML 中的點擊;

const doSomething = document.querySelector('.whateverclassnameyouchoose').addEventListener('click',onClick);

function onClick(e){
what ever you want the function to do
}

希望能幫助到你:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM