...

View Full Version : echo javascript problem



surreal5335
07-25-2008, 05:32 PM
I have a javascript code that worked great by itself, but I try to echo it with php it gives me this parse error:

unexpected T_STRING, expecting ',' or ';' in /home/surreal/public_html/jsheadtag.php on line 8

Here is the string of code in question:



function rollOn(obj){
obj.src=obj.src.replace(/.png/,'blue.png');
}
function rollOff(obj){
obj.src=obj.src.replace(/blue.png/,'.png');
}



Line 8 is the second line of code.

I keep seeing this exact syntax error pop, Is there something I should be aware of to fix this in the future?

ohgod
07-25-2008, 05:34 PM
can you post that code in context please?

ShaneC
07-25-2008, 06:37 PM
Please show us the PHP code, I can't help you if I don't know specifically what you're trying to echo.

JohnDubya
07-25-2008, 08:05 PM
surreal, best thing you could do to avoid PHP echo issues with this chunk of Javascript would be to stop PHP parsing when it gets to this script. Something like this:



<?php
$javascript = 1;

if ($javascript == 1) {
?>

function rollOn(obj){
obj.src=obj.src.replace(/.png/,'blue.png');
}
function rollOff(obj){
obj.src=obj.src.replace(/blue.png/,'.png');
}

<?php
}
?>

ShaneC
07-25-2008, 08:08 PM
surreal, best thing you could do to avoid PHP echo issues with this chunk of Javascript would be to stop PHP parsing when it gets to this script. Something like this:



<?php
$javascript = 1;

if ($javascript == 1) {
?>

function rollOn(obj){
obj.src=obj.src.replace(/.png/,'blue.png');
}
function rollOff(obj){
obj.src=obj.src.replace(/blue.png/,'.png');
}

<?php
}
?>


If you're using a template system, though, that may get a bit hairy.

scoop_987
07-26-2008, 05:49 PM
Since you are echoing it, you MUST escape the " ' "'s otherwise it will fail

Change your PHP part of the JS that you posted to:



echo('function rollOn(obj){
obj.src=obj.src.replace(/.png/,\'blue.png\');
}
function rollOff(obj){
obj.src=obj.src.replace(/blue.png/,\'.png\');
}');

surreal5335
07-26-2008, 08:27 PM
well I tried the parenthesis, it didnt seem to help

I am still getting an error on the same string but now its not giving me any specifics.

Here is the code I am trying to work in its entirty



<?php
echo'

<script language="javascript" type="text/javascript">


function rollOn(obj){
obj.src=obj.src.replace(/.png/,'blue.png');
}
function rollOff(obj){
obj.src=obj.src.replace(/blue.png/,'.png');
}



var currentSpan = 1;
function showMe(n) {
document.getElementById("ex_"+currentSpan).style.display = "none";
document.getElementById("ex_"+n).style.display = "block";
currentSpan = n;
}

var currentDiv = 1;

function showcontent(n) {

// Hide the current side content that is being shown
document.getElementById("SC_"+currentDiv).style.display = "none";

// Show the new side content
document.getElementById("SC_"+n).style.display = "block";

// Set the current open side content
currentDiv = n;
}


function bkgrnd() {
var bg = [];
var n=0;

var base = "http:\/\/i95.photobucket.com/albums/l136/surreal5335/RE%20site/house"

bg[n] = base + "6.png";
bg[n++] = base + "1.png";
bg[n++] = base + "3.png";
bg[n++] = base + "4.png";
bg[n++] = base + "5.png";


var rnd = Math.floor(Math.random() * bg.length) ;

return '<BODY BACKGROUND="' +bg[rnd]+ '"' +' BGCOLOR="#FFFFFF">'
}

</script>';
?>


I am going to be using this code as a seperate file and including it into another php document which is echoing html. I am using php bc of its ability to include external files.

I appreciate the help

Fou-Lu
07-26-2008, 08:31 PM
There is no PHP used within that function, so you can completely remove the php indicators and the echo. If you really want to use it, look into heredoc (http://ca3.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc) syntax; it should reduce the complication on you're escapes. I'd just remove the <?php from it.

surreal5335
07-27-2008, 05:47 PM
Ok I have diagnosed the problem, it lies with with single quote conflicts seeing as the echo is done with single quotes. I made a change to that line and made it all double quotes and that fixed that... there is just one last line I am not sure how to change, it has single and double quotes in it.



return '<BODY BACKGROUND="' +bg[rnd]+ '"' +' BGCOLOR="#FFFFFF">'


Its found on the bottom of the page of code in my last post.

Any suggestions on how to get the single quotes out?

scoop_987
07-27-2008, 07:11 PM
<?php
echo'

<script language="javascript" type="text/javascript">


function rollOn(obj){
obj.src=obj.src.replace(/.png/,\'blue.png\');
}
function rollOff(obj){
obj.src=obj.src.replace(/blue.png/,\'.png\');
}



var currentSpan = 1;
function showMe(n) {
document.getElementById("ex_"+currentSpan).style.display = "none";
document.getElementById("ex_"+n).style.display = "block";
currentSpan = n;
}

var currentDiv = 1;

function showcontent(n) {

// Hide the current side content that is being shown
document.getElementById("SC_"+currentDiv).style.display = "none";

// Show the new side content
document.getElementById("SC_"+n).style.display = "block";

// Set the current open side content
currentDiv = n;
}


function bkgrnd() {
var bg = [];
var n=0;

var base = "http:\/\/i95.photobucket.com/albums/l136/surreal5335/RE&#37;20site/house"

bg[n] = base + "6.png";
bg[n++] = base + "1.png";
bg[n++] = base + "3.png";
bg[n++] = base + "4.png";
bg[n++] = base + "5.png";


var rnd = Math.floor(Math.random() * bg.length) ;

return \'<BODY BACKGROUND="\' +bg[rnd]+ \'"\' +\' BGCOLOR="#FFFFFF">\'
}

</script>';
?>

Should be fixed now

surreal5335
07-28-2008, 01:24 AM
I see what you going for, I tried it but it seems that when I try to view the site it makes a change to the code to look like this:



return&nbsp;\'<BODY&nbsp;BACKGROUND="\'&nbsp;+bg[rnd]+&nbsp;\'"\'&nbsp;+\'&nbsp;BGCOLOR="#FFFFFF">\'


Firebug says its a syntax error

I keep reseting it but it keeps reverting to this messed up everytime.

Any reason why this would be?

Fou-Lu
07-28-2008, 02:08 AM
If firebug says its a syntax error, its now within you're JS code. If you can view it through the source, PHP has done its job.


return \'<BODY BACKGROUND="\\\' +bg[rnd]+ \\\'" BGCOLOR="#FFFFFF">\'

What you're JS sees is \' + ..., you need to escape you're escapes to make this work with inline echos.

surreal5335
07-28-2008, 05:03 PM
Thanks a lot that worked great! One question is there a way to control the repeat and positioning of the background image with javascript?

Fou-Lu
07-28-2008, 09:50 PM
Yeah, use the style object. Since you've just got the tag, you can write it in instead:


return \'<body style="background: #fff url(\\\' + bg[rnd] + \\\') no-repeat fixed center center">\'

This uses CSS which is far more flexible than embedded tag attributes. The above is a shortened background tag which combines background-color, background-image, background-repeat, background-attachment, background-position-x and background-position-y.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum