简体   繁体   English

jQuery Mobile页面加载时运行Javascript函数

[英]Run Javascript function when jquery mobile page loads

Let me start by saying this is kind of a duplicate to this question: 首先,我要说这与这个问题有些重复:

run function when page is loaded . 页面加载时运行函数

But the difference is, while the referenced question includes the javascript function in a html code, i want to know how to use a function from a .js file in my .html file without including the entire function which is quite bulky. 但是不同之处在于,尽管所引用的问题在html代码中包含javascript函数,但我想知道如何在我的.html文件中使用.js文件中的函数,而又不包含整个函数的体积。

I have tested this function by adding it to an event that is triggered when i click a button (function retrieves data from a Google App Engine server) 我已经通过将其添加到单击按钮时触发的事件中来测试了此功能(该功能从Google App Engine服务器检索数据)

$ (document).ready(function() {

$("#showMembers").bind('click', function(event) {
    event.preventDefault();

    doViewMembersRequest();

}); });

What i need is a way to call doViewMembersRequest(); 我需要的是一种调用doViewMembersRequest(); from my viewmembers.html file . 从我的viewmembers.html文件中。

I dont exactly understand what u wanna do! 我不完全明白你想做什么!

But the following code will also work if myFunction is in an .js file, as long this .js file is loaded before the <button> and the function is not declared in an $(function() {}); 但是,如果myFunction在.js文件中,则以下代码也将起作用,只要该.js文件在<button>之前加载并且该函数未在$(function() {}); !

<button onclick="myFunction()">Click me</button>

Just link to the js file in your html page like <script src='yourJsFile.js'></script> 只需链接到html页面中的js文件,例如<script src='yourJsFile.js'></script>

and call the function on ready() 并在ready()上调用该函数

$ (document).ready(function() {
  doViewMembersRequest();
});

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

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