在Internet Explorer中调用ajax后,html onload不会触发,

时间:2014-06-30 13:56:38

标签: javascript php jquery html ajax

我对php脚本进行了ajax调用,该脚本在服务器上运行一些东西。当这个完成后我想让php调出一个javascript函数。为此,我使用了这个。

//This line works in chrome and firefox.
echo "<style onload='test()'></style>";
//this one doesnt work after an ajax call.
echo "<script type='text/javascript'>test()</script>";

此行在chrome和firefox中有效,但在Internet Explorer中则不行。

所以我的问题是,是否有解决方案或替代方案?
有没有办法在ajax调用后调用一个新的javascript函数,具体取决于ajax调用的结果? (不只是成功或失败)

我会向我的系统解释一下额外的信息。我在会话中存储了很多数据。

我在这里得到了我的代码并将其从此示例的额外或无用代码中删除,在此拼写错误期间,可能发生了解析错误或其他语法错误这不是问题

//Index.php
<head><script src='location' type='text/javascript'></script></head>
<body>
   <div id='container'>
      <div id='head'></div>
      <div id='content'></div>
   </div>
   <div id='hidden'></div> // this div is hidden for js feedback
   <script type='text/javascript'>loadContent();</script>
</body>

//javascript file
function loadContent(){
    //my ajax function works it requires 3 variables
    //url to php, div that will be changed, array with post variables.
    Ajax("urltophp", "hidden", Array(""));
}

function loadHome(){
    Ajax("urltophp2", "head", Array("headhome"));
    Ajax("urltophp2", "cont", Array("conthome"));
}

function loadLogin(){
    Ajax("urltophp2", "head", Array("headlogin"));
    Ajax("urltophp2", "cont", Array("contlogin"));
}

function test(){
    alert("test"); //for testing
}

//then urltophp
$ses = $_SESSION("ses"); //this is an object of class SESSION
$timeout = $ses->getTimeout();
$loggedin = $ses->getLoggedin();
//THIS WORKS IN CHROME AND FIREFOX NOT IN IE.
if(time() - $timeout < 3600){  //if an hour hasnt passed since last action.
    if($loggedin){             //if user was logged in.   
        echo "<style onload='loadHome()'></style>";
    }else{
        echo "<style onload='loadLogin()'></style>";
    }
}

//urltophp2
$p = $_POST['var1'];
$ses = $_SESSION["ses"];
//used for checking if user is logged in before loading page 
//in case of cross-site scripting. left out in this example.
$loggedin = $ses->getloggedin();
switch($p){
    case "headhome":
    echo $homehead;  //assume I loaded this from a file.
    break;
    case "conthome":
    echo $conthead;  //assume the same.
    break;
    etc. for all options.
}

提前感谢kpp。

1 个答案:

答案 0 :(得分:0)

根据ajax调用的结果,有没有办法在ajax调用后调用新的javascript函数?

$.get("test.php", function(data) {
    if (data.prop === "A") {
        foo();
    } else {
        bar();
    }
});

注意:显然我在这里使用jQuery。