简体   繁体   中英

Echo a php function in a javascript

I have the following javascript

function showIt(item,i,j,max){
  var id;
  actualItem=item;
  for(var x=1;x<=i;x++){
    id=item+"_"+x;
    document.getElementById(id).src="<?= suburl(); ?>/b.gif";
  }
  for(var x=i+1;x<=max;x++){
    id=item+"_"+x;
    if(x<=j)
      document.getElementById(id).src="<?= suburl(); ?>/y.gif";
    else document.getElementById(id).src="<?= suburl(); ?>/w.gif";
  }
}

I have tried with <?= suburl(); ?> <?= suburl(); ?> and with <?php echo suburl(); ?> <?php echo suburl(); ?>

Function suburl(); looks like this:

function suburl() {
  $suburl = 'http://localhost/www.site.com/static';
  echo $suburl;
}

And it's not working on echoing it in javascript. It appears like pure html. It's much appreciated thank you.

You could try with something like this:

function showIt(item,i,j,max){
  var id;
  actualItem=item;
  var url = suburl(); //THIS IS NEW
  for(var x=1;x<=i;x++){
    id=item+"_"+x;
    document.getElementById(id).src=url+"/b.gif";
  }
  for(var x=i+1;x<=max;x++){
    id=item+"_"+x;
    if(x<=j)
      document.getElementById(id).src=url+"/y.gif";
    else document.getElementById(id).src=url+"/w.gif";
  }
}

function suburl() {
  var suburl = 'http://localhost/www.site.com/static';
  return suburl;
}
function showIt(item,i,j,max){
var id;
actualItem=item;
for(var x=1;x<=i;x++){
id=item+"_"+x;
document.getElementById(id).src="<?= echo suburl(); ?>/b.gif";
}
for(var x=i+1;x<=max;x++){
  id=item+"_"+x;
  if(x<=j)
  document.getElementById(id).src="<?= echo suburl(); ?>/y.gif";
else document.getElementById(id).src="<?= echo suburl(); ?>/w.gif";
}
}

Function suburl(); looks like this:

function suburl() {
$suburl = 'http://localhost/www.site.com/static';
return $suburl;
}

Hope this Help. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM