简体   繁体   English

如何在Pageload期间调用JavaScript函数?

[英]How to call JavaScript function during Pageload?

I have a Html page contains several pages whose data-role = page as page1,page2 etc. I am trying to call a JS method during pageload of page1 using the following code 我有一个HTML页面,其中包含几个页面,其data-role =页面为page1,page2等。我试图使用以下代码在page1的页面加载期间调用JS方法

$("#page1").on("load",function () {
  alert("hi")
  $.ajax({
          type: "GET",
          url: "",
          data: "{}",
          contentType: "application/json",
          dataType:"json", 
          success:  function (msg) {
                                    var BPRList = '';
                                    $.each(msg, function(i,v){  
                                      BPRList += '<li onClick="GetBprDetails('+ v.BPRNo +')"><a href="#bprPage" data-transition="slide"><p class="title">' + v.BPRNo + '</p><p class="bodyEle">' + v.BPR_Product +'</p><p class="bodyEle">' + v.BPR_Details+ '</p><br/><p class="bodyEle">' + v.BPR_Status + ':</p></a></li>'     
                                                             })         
                                   $("#BPRListTable").html(BPRList)
                                   $('[data-role=listview]').listview('refresh');
                                   },
           error:    function () {
                                   alert("BPR List Error");
                                 }
        }); });

During the execution of above function I am unable to get the output during the formload where as if I call the above method as button click event am able to get the output. 在执行上述功能期间,我无法在formload期间获取输出,就好像我调用上述方法一样,因为按钮单击事件能够获取输出。 What might be the mistake made in above code.. I am more worried in following code. 上面的代码中可能犯了什么错误。.我更担心下面的代码。

$("#page1").on("load",function () {
 --statements
})
 $(window).load(function () {
   alert("hi")
 });

If you want the results on page load,then try this instead.Write this code outside $(document).ready(function(){}); 如果您希望页面加载结果,请尝试使用此方法。在$(document).ready(function(){});之外编写此代码$(document).ready(function(){});

Try to put 'page:load' instead of 'load' it might work better : 尝试放置'page:load'而不是'load'可能会更好:

$("#page1").on("page:load",function () {
    - - - - 
    - - - -
    - - - - 
});

Found solution Myself 找到解决方案

 $(document).ready(function(){
 $(document).on('pagebeforeshow','#page1',function () {
                                                           --statements
                                                          }); });

i think using pageinit would be ok 我认为使用pageinit可以

$(document).on('pageinit', '#page1', function() {...})

In this manner, you avoid using document.ready, which is also not recommended by jquery mobile. 通过这种方式,您避免使用document.ready,jquery mobile也不建议这样做。

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

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