Javascript 在index.html中加载时调用js函数时出现问题
我想在index.html加载时调用js函数 此js函数在main.js文件中定义 我可以用下面的方式来调用它Javascript 在index.html中加载时调用js函数时出现问题,javascript,jquery,ibm-mobilefirst,Javascript,Jquery,Ibm Mobilefirst,我想在index.html加载时调用js函数 此js函数在main.js文件中定义 我可以用下面的方式来调用它 <input type="button" value="Submit" onclick="getSecretData()" /> 但是我希望每次加载index.html时都调用这个函数(而不是按钮) 我尝试了下面的代码。它不起作用。你能帮忙吗 index.html <script>
<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));
}
index.html
$(文档).ready(函数(){
getSecretData();
});
main.js
函数getSecretData(){
变量调用数据={
适配器:“DummyAdapter”,
过程:“getSecretData”,
参数:[]
};
WL.Client.InvokeProcess(调用数据{
onSuccess:getSecretData\u回调,
onFailure:getSecretData\u回调
});
}
函数getSecretData\u回调(响应){
警报(“getSecretData_回调响应::”+JSON.stringify(响应));
}
谢谢你正在使用Worklight
这里给出的其他答案可能适用于您,但由于您使用的是Worklight,因此您应该使用更适合基于Worklight的应用程序的方法
在Worklight应用程序的common\js\main.js中,有wlCommonInit()
如果你想在启动应用程序时运行一些代码,请将其放在那里
function wlCommonInit() {
myFunction();
}
function myFunction(){
// whatever you want to run...
}
但请注意,在应用程序启动时调用适配器是不明智的。适配器需要连接到Worklight Server,因此您要做的是首先尝试使用WL.Client.connect
连接到服务器,如果成功连接,则只能通过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(){
// ...
}
你可以试试这个
document.querySelector('body').addEventListener("load", getSecretData, false);
要了解更多信息,我建议您阅读或啊,好的..没按门铃。谢谢