2015-05-04 54 views
0

我有一个html文件,我需要从服务器加载一些项目,为此我在js文件中使用了一个函数。现在,我可以在js文件中使用该函数,而不是在HTML文件的标记中包含该代码,并且可以通过HTML中的任何标记(只要页面加载)上的onload属性来调用它。如何在html中使用onload事件来调用js文件中的函数

我的HTML页面:

<script> 
$.getScript(path + "js/jsfile.js"); 
</script> 

<link rel="stylesheet" href="jqueryMobile/jquery.mobile-1.4.5.min.css"> 
<script src="jqueryMobile/jquery-1.11.2.min.js"></script> 
<script src="jqueryMobile/jquery.mobile-1.4.5.min.js"></script> 

<header data-role="header"> 
    <a href="#" class="ui-btn ui-icon-back ui-btn-icon-left" 
    data-role="button" onclick="currentPage.back();">Back</a> 
    <!--some header tags were here--> 
</header> 

<div data-role="content" style="padding: 15px" > 
    <!--some button tags were here--> 
</div> 

只要这个页面被加载时,我需要的getColor()函数来执行,得到的结果取到HTML页面。

我jsfile.js页:

currentPage = {}; 

currentPage.init = function() { 
    WL.Logger.debug("listofenvironments :: init"); 
}; 

currentPage.back = function(){ 
    WL.Logger.debug("Login :: back"); 
    pagesHistory.push(path + "pages/otherpage.html"); 
    $("#pagePort").load(path + "pages/" + "otherpage.html"); 
}; 

function getColor(){ 
    //some coding goes on here.... 
} 

我已经用在我的HTML页面中的标签的onload事件试过,但它不工作。而currentPage.back()函数工作正常。

+0

你确定所有方法一般工作吗? – reporter

回答

2

添加以下线路在jsfile.js

$(document).ready(function(){ 
getColor();//ready function will be executed after document elements are loaded. 
}); 

希望这会为你工作。

2

肯蒂经历一些教程。这些都是基本的问题,你不必寻找计算器。然而找到答案

<body onload="myFunc()"> 
</body> 

//in your jsfile 
function myFunc() { 
    //your code 
} 

的onLoad得到您的DOM,两种资源后调用下面获取加载,如果你想DOM装载和资源不需要加载使用jQuery就绪功能

后才被调用的函数
$(document).ready(function(){ 
    //your code 
}); 
相关问题