是否可以从Ajax响应更改Javascript变量?

是否可以从Ajax响应更改Javascript变量?,javascript,events,variables,Javascript,Events,Variables,我试图更新一个Javascript变量,该变量根据一些Ajax代码控制滚动脚本。当Ajax脚本中的条件为true时,Ajax代码应该添加到jsvar中。有没有办法触发这个 谢谢 编辑:我不确定如何更改变量的值。我尝试过通过Ajax更改它,但没有得到解析。我还尝试在JS中使用PHP来检查条件,但这样做只起一次作用 JS代码 function speedUp() { actualheight = actualheight + 50; } function slowDown(

我试图更新一个Javascript变量,该变量根据一些Ajax代码控制滚动脚本。当Ajax脚本中的条件为true时,Ajax代码应该添加到jsvar中。有没有办法触发这个

谢谢

编辑:我不确定如何更改变量的值。我尝试过通过Ajax更改它,但没有得到解析。我还尝试在JS中使用PHP来检查条件,但这样做只起一次作用

JS代码

function speedUp()
    {
    actualheight = actualheight + 50;
    }

function slowDown()
{
actualheight = actualheight - 50;
}

function ajaxFunction()
{
var xmlhttp = createRequestObject();
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {
   document.getElementById('iemarquee').innerHTML=xmlhttp.responseText;
   document.getElementById('iemarquee2').innerHTML=xmlhttp.responseText;
  }
}
xmlhttp.open("GET","saleCallTest.php",true);
xmlhttp.send(null);
}

/*
Cross browser Marquee II- © Dynamic Drive (www.dynamicdrive.com)
For full source code, 100's more DHTML scripts, and TOS, visit http://www.dynamicdrive.com
Modified by jscheuer1 for continuous content. Credit MUST stay intact for use
*/

//Specify the marquee's width (in pixels)
var marqueewidth="500px"
//Specify the marquee's height
var marqueeheight="500px"
//Specify the marquee's marquee speed (larger is faster 1-10)
var marqueespeed=1
//Specify initial pause before scrolling in milliseconds
var initPause=1000
//Specify start with Full(1)or Empty(0) Marquee
var full=1
//Pause marquee onMousever (0=no. 1=yes)?
var pauseit=0
//Specify Break characters for IE as the two iterations
//of the marquee, if text, will be too close together in IE
var iebreak='<p></p>'

//Specify the marquee's content
//Keep all content on ONE line, and backslash any single quotations (ie: that\'s great):

var marqueecontent='<?php for($i=0;$i<=count($saleItems);$i++)
{
if ($saleItems[$i]['stateOfItem'] =="Sold" || $saleItems[$i]['stateOfItem'] =="Unsold")
{
$_SESSION['countItems']++;

echo $saleItems[$i]['itemNumber'];
echo $saleItems[$i]['stateOfItem'] . '<br />';

}};
?>'


////NO NEED TO EDIT BELOW THIS LINE////////////
var copyspeed=marqueespeed
var pausespeed=(pauseit==0)? copyspeed: 0
var iedom=document.all||document.getElementById
var actualheight=''
var cross_marquee, cross_marquee2, ns_marquee

function populate(){
if (iedom){
var lb=document.getElementById&&!document.all? '' : iebreak
cross_marquee=document.getElementById? document.getElementById("iemarquee") : document.all.iemarquee
cross_marquee2=document.getElementById? document.getElementById("iemarquee2") : document.all.iemarquee2
cross_marquee.style.top=(full==1)? '8px' : parseInt(marqueeheight)+8+"px"
cross_marquee2.innerHTML=cross_marquee.innerHTML=marqueecontent+lb
actualheight=cross_marquee.offsetHeight
cross_marquee2.style.top=(parseInt(cross_marquee.style.top)+actualheight+8)+"px" //indicates following #1

}
else if (document.layers){
ns_marquee=document.ns_marquee.document.ns_marquee2
ns_marquee.top=parseInt(marqueeheight)+8
ns_marquee.document.write(marqueecontent)
ns_marquee.document.close()
actualheight=ns_marquee.document.height

}
setTimeout('lefttime=setInterval("scrollmarquee()",20)',initPause)
}
window.onload=populate

function scrollmarquee(){

if (iedom){
if (parseInt(cross_marquee.style.top)<(actualheight*(-1)+8))
cross_marquee.style.top=(parseInt(cross_marquee2.style.top)+actualheight+8)+"px"
if (parseInt(cross_marquee2.style.top)<(actualheight*(-1)+8))
cross_marquee2.style.top=(parseInt(cross_marquee.style.top)+actualheight+8)+"px"
cross_marquee2.style.top=parseInt(cross_marquee2.style.top)-copyspeed+"px"
cross_marquee.style.top=parseInt(cross_marquee.style.top)-copyspeed+"px"
}

else if (document.layers){
if (ns_marquee.top>(actualheight*(-1)+8))
ns_marquee.top-=copyspeed
else
ns_marquee.top=parseInt(marqueeheight)+8
}
}

if (iedom||document.layers){
with (document){
if (iedom){
write('<div style="position:relative;width:'+marqueewidth+';height:'+marqueeheight+';overflow:hidden" onMouseover="copyspeed=pausespeed" onMouseout="copyspeed=marqueespeed">')
write('<div id="iemarquee" style="position:absolute;left:0px;top:0px;width:100%;background:black;color:white;font-size:30pt;">')
write('</div><div id="iemarquee2" style="position:absolute;left:0px;top:0px;width:100%;z-index:100;background:black;color:white;font-size:30pt;">')
write('</div></div>')

}
else if (document.layers){
write('<ilayer width='+marqueewidth+' height='+marqueeheight+' name="ns_marquee">')
write('<layer name="ns_marquee2" width='+marqueewidth+' height='+marqueeheight+' left=0 top=0 onMouseover="copyspeed=pausespeed" onMouseout="copyspeed=marqueespeed"></layer>')
write('</ilayer>')
}
}
}


</script>
函数加速()
{
实际高度=实际高度+50;
}
函数(
{
实际高度=实际高度-50;
}
函数ajaxFunction()
{
var xmlhttp=createRequestObject();
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4)
{
document.getElementById('iemarquee')。innerHTML=xmlhttp.responseText;
document.getElementById('iemarquee2')。innerHTML=xmlhttp.responseText;
}
}
open(“GET”,“saleCallTest.php”,true);
xmlhttp.send(空);
}
/*
跨浏览器Marquee II-©Dynamic Drive(www.dynamicdrive.com)
有关完整的源代码、100多个DHTML脚本和TOS,请访问http://www.dynamicdrive.com
由jscheuer1修改,用于连续内容。信用证必须完好无损才能使用
*/
//指定选框的宽度(以像素为单位)
var marqueewidth=“500px”
//指定选框的高度
var marqueehight=“500px”
//指定选框的选框速度(越大1-10越快)
var marqueespeed=1
//指定滚动前的初始暂停(毫秒)
var initPause=1000
//指定从满(1)或空(0)选框开始
var full=1
//在Mousever上暂停选框(0=否。1=是)?
var pauseit=0
//将IE的中断字符指定为两个迭代
//如果文字在IE中过于靠近,则选框的
变量iebreak='

' //指定选框的内容 //将所有内容放在一行,并将任何引用反斜杠(即:太好了): var marqueecontent=“” ////无需在此行下方进行编辑//////////// var copyspeed=marqueespeed var pausespeed=(pauseit==0)?复制速度:0 var iedom=document.all | | document.getElementById 变量实际高度=“” var cross_marquee,cross_marquee 2,ns_marquee 函数填充(){ 如果(iedom){ var lb=document.getElementById&!document.all?'':iebreak cross_marquee=document.getElementById?document.getElementById(“iemarquee”):document.all.iemarquee cross_marquee2=document.getElementById?document.getElementById(“iemarquee2”):document.all.iemarquee2 cross_marquee.style.top=(full==1)?'8px':parseInt(marqueehight)+8+“px” cross_marquee2.innerHTML=cross_marquee.innerHTML=marqueecontent+lb 实际高度=交叉字幕 cross_marquee2.style.top=(parseInt(cross_marquee.style.top)+实际高度+8)+“px”//表示以下#1 } else if(document.layers){ ns_字幕=document.ns_字幕.document.ns_字幕2 ns_marquee.top=parseInt(选框高度)+8 ns_marquee.document.write(marqueecontent) ns_marquee.document.close() 实际高度=ns_marquee.document.height } setTimeout('lefttime=setInterval(“scrollmarquee()”,20)”,initPause) } window.onload=填充 函数scrollmarquee(){ 如果(iedom){ if(parseInt(cross_marquee.style.top)PHP代码


假设变量没有锁定在不同的名称空间中,并且没有可用的接口,那么当然

“Ajax”只是指“使用JS从服务器获取一些数据而不离开页面,然后运行一些JS”


没有什么特别的东西会给JS的功能增加额外的限制。

我也遇到了一些问题。可能是名称空间问题,如这里的另一个答案所示。
我没有弄清楚什么/什么时候/为什么,而是使用了
,然后用Javascript更新并阅读:

然后,编写变量:
document.getElementById('blah').value='somenewvalue';

要读取变量:
somevar=document.getElementById('blah').value;

事实上,找出正确的名称空间是一个更好的选择,但这是可行的

编辑:您是使用任何Javascript库来为自己编写ajax,还是从头开始编写?我使用过xajax、Prototype和Jquery来实现类似的功能。Jquery是我的新宝贝,但5年前在xajax中已经非常简单了

我不确定我是否想引导您走这条路,但对于一个php程序员来说,xajax是一个非常简单的学习方法

EDIT2:据我所知,您正在返回要在同一响应中执行的HTML和javascript。在响应中包含javascript不会使其得到执行。也许您应该使用JSON序列化您的返回,以便您可以评估要执行的javascript,并分别分配您的innerHTML。 仅供参考,与您在xajax中使用以下工具可以执行的操作相同:

$objResponse->addAssign("idhere","innerHTML", "some html");
$objResponse->addScript("somvar = somevar + someothervar");

我也尝试过类似的方法。我有一个隐藏的输入,它被设置为调用我需要更改变量的函数,但是,我不知道如何调用它。()我看不出onclick事件对隐藏字段有什么帮助。我的意思是让隐藏字段本身成为变量。也许你有一个范围问题?你的处理程序在“查找”方面有困难吗要更新的变量?
$objResponse->addAssign("idhere","innerHTML", "some html");
$objResponse->addScript("somvar = somevar + someothervar");