【发布时间】: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