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

时间:2009-12-07 17:35:51

标签: javascript events variables

我正在尝试更新一个基于某些Ajax代码控制滚动脚本的Javascript变量。当Ajax脚本中的条件为真时,Ajax代码应该添加到JS Var。有没有办法触发这个?

谢谢!

编辑:我不确定如何更改变量的值。我试过通过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>

AJAX-&gt; PHP代码

<?php
session_start();

//NuSoap Library
require_once('./lib/nusoap.php');
$_SESSION['countTotal'] = 0;

//Creating a Client
$client = new nusoap_client('http://xx.xx.x.xxx:xxxx/WSSERVICE/services/SERVICE?WSDL');



$saleItems= $client->call("getItems", array("Sale" => '001'));

$_SESSION['countNew'] = 0;


$countPresale = count($saleItems);
$timer = $countPresale * 3.15;

 for($i=0;$i<=count($saleItems);$i++)
{
if ($saleItems[$i]['stateOfItem'] =="Sold" || $saleItems[$i]['stateOfItem'] =="Unsold")
{
$_SESSION['countNew']++;

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

}};

if($_SESSION['countNew'] < $_SESSION['countVehicles'])
{
$_SESSION['countTotal']--;
}

if($_SESSION['countNew'] > $_SESSION['countVehicles'])
{
$_SESSION['countTotal']++;
}


if($_SESSION['countTotal'] < 0)
{
$opx = 3 * ($_SESSION['countItems'] - $_SESSION['countNew']);
echo 'actualheight = parseInt(actualheight) + parseInt(' . $opx . ');';
$_SESSION['countVehicles'] = $_SESSION['countNew'];

}

if($_SESSION['countTotal'] > 0)
{
$opx = 3 * ($_SESSION['countItems'] + ($_SESSION['countNew'] - $_SESSION['countNew']));
echo 'actualheight = parseInt(actualheight) - parseInt(' . $opx . ');';
$_SESSION['countItems'] = $_SESSION['countNew'];

}



?>

2 个答案:

答案 0 :(得分:1)

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

“Ajax”只是意味着“在不离开页面的情况下使用JS从服务器获取一些数据,然后运行一些JS”。

没有什么特别的东西可以为JS所能做的事情增加额外的限制。

答案 1 :(得分:0)

我也遇到了一些问题。可能存在命名空间问题,如此处的另一个答案所示 我没有弄清楚什么/何时/为什么,而是使用<input name="blah" type=hidden>然后更新它并使用Javascript读取它:

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

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

每次都有效。实际上找出正确的命名空间将是一个更好的选择,但这是有效的。

编辑:您是否正在使用任何Javascript库为您执行ajax,或者只是从头开始编写代码?我已经将xajax,Prototype和Jquery用于这样的事情。 Jquery是我的新生儿,但5年前已经在xajax中已经死了。

我不确定我是想引导你走这条路,但对于一个php程序员来说,xajax是一个非常简单的学习方法。在我看来,Jquery更强大,更具扩展性。

EDIT2:我可以告诉你,正在返回HTML和javascript以在同一个响应中执行。在您的回复中包含javascript不会使其执行。也许您应该使用JSON序列化您的返回,这样您就可以评估要执行的javascript,并分别分配您的innerHTML。 仅供参考,您可以在xajax中执行相同的操作,只需:

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