Javascript 执行后重定向位置错误
我已经思考了几个小时了,看着这个屏幕,我几乎近视了,我不知道是什么导致了这个问题 我可以创建一个脚本,该脚本在执行后重定向到一个唯一的页面(在变量中定义)。脚本工作正常,但每次重定向到的位置都是错误的 比如说。一个用户有4个链接要检查,假设他们是他/她的页面上的新评论。当他/她点击链接时,每次都会指向最老的链接。不是他们点击的那个 我有两部分代码:Javascript 执行后重定向位置错误,javascript,php,url-redirection,Javascript,Php,Url Redirection,我已经思考了几个小时了,看着这个屏幕,我几乎近视了,我不知道是什么导致了这个问题 我可以创建一个脚本,该脚本在执行后重定向到一个唯一的页面(在变量中定义)。脚本工作正常,但每次重定向到的位置都是错误的 比如说。一个用户有4个链接要检查,假设他们是他/她的页面上的新评论。当他/她点击链接时,每次都会指向最老的链接。不是他们点击的那个 我有两部分代码: <? $result_s = mysql_query("SELECT * FROM pins a LEFT JOIN comments b O
<?
$result_s = mysql_query("SELECT * FROM pins a LEFT JOIN comments b ON a.id = b.pin_id WHERE b.to_id = '$myid' AND b.status = '0' ORDER BY date DESC");
$rows = array();
while ($row = mysql_fetch_assoc($result_s)) {
$jS = '"'.$row[id].'"';
$boardid = $row['board_id']; // collection id
$postid = $row['pin_id']; // post id
$posturl = $row['pin_url']; // post id
echo "<table width='100%'><tr>";
echo "<td align='center'><a href='javascript:setActive(".$jS.")'><img src='".$posturl."' height='100'><br>".$boardid."/".$postid."</a></td>";
echo "</tr></table>";
}
?>
有几个问题
Ajax中的A代表异步。因此,在.send之后立即执行的任何操作都是在Ajax调用之前执行的(XMLHttpRequest的使用称为Ajax)
您使用ObjID调用函数,然后在Ajax调用的返回中对其进行操作(xmlhttp.onreadystatechange中的任何内容),但由于ObjID不是全局的,并且没有复制到作用域(闭包)中,因此在该函数中是未知的
试试这个
function setActive(ObjID) {
var xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
new ActiveXObject("Microsoft.XMLHTTP");
var ID = ObjID
xmlhttp.onreadystatechange = function(id) {
return function() {
if (this.readyState != 4) return;
if (this.status == 200) {
var obj = document.getElementById(id); // id is passed now
var style = obj.style; // but please use CSS intead
style.color = style.color == 'black'?'red':'black';
style.fontFamily = "Century Gothic";
style.textAlign="center";
obj.innerHTML = xmlhttp.responseText;
setTimeout(function() {
location.href="/board/pins/<?php echo $boardid; ?>/<?php echo $postid; ?>"
},3000); // assuming this is what you want
};
}(ID); // passing the ObjID
}
var p_str = "a="+ObjID;
xmlhttp.open("POST","/application/views/setActive.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(p_str);
}
函数setActive(ObjID){
var xmlhttp=window.XMLHttpRequest?新的XMLHttpRequest():
新的ActiveXObject(“Microsoft.XMLHTTP”);
var ID=ObjID
xmlhttp.onreadystatechange=函数(id){
返回函数(){
如果(this.readyState!=4)返回;
如果(this.status==200){
var obj=document.getElementById(id);//现在已传递id
var style=obj.style;//但请使用CSS intead
style.color=style.color==‘黑色’?‘红色’:‘黑色’;
style.fontfamine=“世纪哥特式”;
style.textAlign=“中心”;
obj.innerHTML=xmlhttp.responseText;
setTimeout(函数(){
location.href=“/board/pins/”
},3000);//假设这是你想要的
};
}(ID);//传递对象
}
var p_str=“a=”+ObjID;
open(“POST”,“/application/views/setActive.php”,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xmlhttp.send(p_str);
}
您必须更改ajax返回部分中的href。但这样做就没有必要在那里做任何其他事情。如果您想让用户看到更改,然后更改href,您需要使用setTimeout(function(){location='…';},3000)例如,您的意思是在setActive.php中请求$boardid和$postid,覆盖脚本中的位置?我不想让用户看到它,应该是seemless谢谢您的建议。但是,此返工根本不允许单击。点击没有任何作用。
function setActive(ObjID) {
var xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
new ActiveXObject("Microsoft.XMLHTTP");
var ID = ObjID
xmlhttp.onreadystatechange = function(id) {
return function() {
if (this.readyState != 4) return;
if (this.status == 200) {
var obj = document.getElementById(id); // id is passed now
var style = obj.style; // but please use CSS intead
style.color = style.color == 'black'?'red':'black';
style.fontFamily = "Century Gothic";
style.textAlign="center";
obj.innerHTML = xmlhttp.responseText;
setTimeout(function() {
location.href="/board/pins/<?php echo $boardid; ?>/<?php echo $postid; ?>"
},3000); // assuming this is what you want
};
}(ID); // passing the ObjID
}
var p_str = "a="+ObjID;
xmlhttp.open("POST","/application/views/setActive.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(p_str);
}