Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    echo javascript problem

    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:

    Code:
    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?

  • #2
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    can you post that code in context please?

  • #3
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Please show us the PHP code, I can't help you if I don't know specifically what you're trying to echo.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #4
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    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 Code:
    <?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
    }
    ?>

  • #5
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Quote Originally Posted by JohnDubya View Post
    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 Code:
    <?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.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #6
    New Coder
    Join Date
    Jul 2008
    Posts
    91
    Thanks
    4
    Thanked 9 Times in 9 Posts
    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:

    PHP Code:
    echo('function rollOn(obj){ 
    obj.src=obj.src.replace(/.png/,\'blue.png\'); 

    function rollOff(obj){ 
    obj.src=obj.src.replace(/blue.png/,\'.png\'); 
    }'
    ); 
    Last edited by JohnDubya; 07-26-2008 at 05:51 PM. Reason: Rude remark removed

  • #7
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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

    Code:
    <?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

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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 syntax; it should reduce the complication on you're escapes. I'd just remove the <?php from it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #9
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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.

    Code:
    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?

  • #10
    New Coder
    Join Date
    Jul 2008
    Posts
    91
    Thanks
    4
    Thanked 9 Times in 9 Posts
    PHP Code:
    <?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>'
    ;
    ?>
    Should be fixed now

  • #11
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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:

    Code:
    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?
    Last edited by surreal5335; 07-28-2008 at 01:27 AM. Reason: fixed another problem

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    Code:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #13
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks a lot that worked great! One question is there a way to control the repeat and positioning of the background image with javascript?

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yeah, use the style object. Since you've just got the tag, you can write it in instead:
    Code:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •