【问题标题】:html onload not triggering after ajax call in Internet Explorer,在 Internet Explorer 中调用 ajax 后未触发 html onload,
【发布时间】:2014-06-30 13:56:38
【问题描述】:

我有一个对 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 调用后根据 ajax 调用的结果调用新的 javascript 函数? (不只是成功或失败)

我会更多地解释我的系统以获取更多信息。我在一个会话中存储了很多数据。

我在这里得到了我的代码,并从这个例子中多余或无用的代码中剥离了它,在这个拼写错误期间,可能发生了解析错误或其他语法错误这不是问题。。 p>

//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。

【问题讨论】:

  • 我认为onload 不是style 标签的属性
  • onload='test()'移动到正文标签
  • @colburton 我试过了,在任何浏览器上都不起作用。最大的问题是我在加载 dom 后尝试执行 javascript,而 IE 告诉我这是不可能的,而其他人则肯定会继续

标签: javascript php jquery html ajax


【解决方案1】:

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

是的。

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

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

【讨论】:

  • 我只是在调查这个问题,但剩下的唯一问题是我如何在那里进行检查,比如我可以让我的 php 返回像 1、2 或 3 一样,但仍然回显其他东西在一个div中?还是我必须让 php echo 1, 2, 3 然后使用该结果在 div 中回显其他内容?
  • 我不确定你的意思。要将数据从 PHP 传递到 JS,您应该使用内置的 json_encode 函数。然后像&lt;script&gt;var jsondata = &lt;?php echo data ?&gt; &lt;/script&gt;这样回显函数的结果。
  • 我的问题表述错误,我的意思是data.prop 来自哪里。这是您在 php 中回显的内容还是返回的内容。
猜你喜欢
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
相关资源
最近更新 更多