gpt4 book ai didi

javascript - 在 index.html 中加载时调用 js 函数时出现问题

转载 作者:行者123 更新时间:2023-11-28 08:00:22 26 4
gpt4 key购买 nike

我想在加载index.html时调用js函数。

这个js函数在main.js文件中定义。

我可以使用以下方式调用它

           <input type="button" value="Submit" onclick="getSecretData()" />

但我希望每次加载index.html(而不是按钮)时调用此函数

我尝试了下面的代码。它不起作用。你能帮忙吗?

                index.html 

<script>
$(document).ready(function() {

getSecretData();


});
</script>


main.js

function getSecretData(){
var invocationData = {
adapter: "DummyAdapter",
procedure: "getSecretData",
parameters: []
};

WL.Client.invokeProcedure(invocationData, {
onSuccess: getSecretData_Callback,
onFailure: getSecretData_Callback
});
}

function getSecretData_Callback(response){
alert("getSecretData_Callback response :: " + JSON.stringify(response));
}

谢谢

最佳答案

您正在使用 Worklight。 Did you read the training materials

此处给出的其他答案可能适合您,但由于您使用的是 Worklight,因此您应该采用更适合基于 Worklight 的应用程序的方法。

在您的 Worklight 应用程序的 common\js\main.js 中,有 wlCommonInit()
如果您希望在启动应用程序时运行某些代码,请将其放置在那里。

function wlCommonInit() {
myFunction();
}

function myFunction(){
// whatever you want to run...
}

但请注意,在应用程序启动时调用适配器并不明智。适配器需要连接到 Worklight Server,因此您要做的就是首先尝试使用 WL.Client.connect 连接到服务器,如果成功,则只能通过以下方式调用适配器连接的 onSuccess 回调。

WL.Client.connect({onSuccess: myFunction, onFailure: connectionFailure});

例如:

function wlCommonInit() {
WL.Client.connect({
onSuccess: myFunction,
onFailure: connectionFailure
});
}

function myFunction(){
// whatever you want to run...
}

function connectionFailure(){
// ...
}

关于javascript - 在 index.html 中加载时调用 js 函数时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25509096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com