开发者

how to use js function from loaded ajax?

the function is in the page loading the ajax but i'm trying to call the function

codes:

[ajax]

$.ajax({
type: "POST",
url: "loginpersonal.asp",
data: "id=<%=request("id")%>",
beforeSend: function() {
    $("#personaltab").hide();
},
success: function(msg){
    $("#personaltab").empty().append(msg);
},
complete: function() {
    $("#personaltab").slideDown();
},
error: function() {
    $("#personaltab").append("error").slideDown();
}
});

[the js function]

function GetCount(t){
    if(t>0) {
        total = t
    }
    else {
        total -=1;
    }
    amount=total;
                                            if(amount < 0){
        startpersonalbid();
    }
    else{
        days=0;hours=0;mins=0;secs=0;out="";
        days=Math.floor(amount/86400);//days
        amount=amount%86400;
        hours=Math.floor(amount/3600);//hours
        amount=amount%3600;
        mins=Math.floor(amount/60);//minutes
        amount=amount%60;

        secs=Math.floor(amount);//seconds
        if(days != 0){out += days +":";}
        if(days != 0 || hours != 0){out += hours +":";}
        if(days != 0 || hours != 0 || mins != 0){out += ((mins>=10)?m开发者_如何学编程ins:"0"+mins) +":";}
        out += ((secs>=10)?secs:"0"+secs) ;
        document.getElementById('countbox').innerHTML=out;
        setTimeout("GetCount()", 1000);
    }
}
window.onload=function(){
GetCount(<%= DateDiff("s", Now,privatesellstartdate&" "&privatesellstarttime ) %>);

so at the end of the loginpersonal.asp from the ajax... if it does what it suppose to do... i'm trying to call the function GetCount() again.


you cannot call to javascript included on ajax pages for security. you need to call the external javascript and eval it into the application. using a framework, many frameworks do this automatically.


I'm not sure what library you are using, but I use HTML like the following:

<div>
  My markup
</div>

<script type="text/javascript">

(function ($) {
   window.myFunction = function() {
      alert("Running from ajax loaded file");
   }
})(jQuery);

</script>

And then with jquery, I call it like this:

$("div#loadMe").load("/snippets/file.html", function(response, status, xhr){
  if (status == "error") {
    alert ("Error");
    return;
  }
  // do stuff
  window.myFunction();
});

Of course, you could use whatever library you like to do the ajax loading. I haven't tested the code above, but it is similar to code I have in production now.

I believe that with jquery, you have to load the ENTIRE file, or the scripts won't run. If you target just a certain element within a file, the scripts don't get loaded or run. In other words, you can't do this: load("/snippets/file.html #container") and have your scripts run.

Also, I think you must be on the same site for this to work, unless you are loading using jsonp.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜