1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Dynamic changing of div content using javascript issue

Discussion in 'JavaScript and AJAX' started by rekha, Jan 7, 2010.

  1. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    Anyone pls tell me where is the issue?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <?php
    $ss='<script type="text/javascript"><!--
    google_ad_client = "pub-3229609591361912";
    /* 728x90,created 5/2/08 */
    google_ad_slot = "0355456913";
    google_ad_width = 728;
    google_ad_height = 90;
    //-->
    </script>
    <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>';
    //echo $ss;
    ?>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <div id="container">
    This is the default text
    </div>
    <script type='text/javascript'>
    window.onload = function(){
    var d = document.getElementById('container');
    
    var def = d.childNodes[0].nodeValue;
    
    t = 5000;
    
    setTimeout(function(){
    d.childNodes[0].nodeValue ='<?php 'echo $ss;'?>';alert('<?php 'echo $ss;'?>');
    setTimeout(function(){
    d.childNodes[0].nodeValue = def;
    setTimeout(function(){
    d.childNodes[0].nodeValue = '';
    }, t);
    }, t);}, t);
    };
    </script>
    </body>
    </html>
     
  2. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Pls someone help me to solve this issue.
     
  3. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    PHP:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <?php

    $ss
    ='</script><script type="text/javascript"><!--
    google_ad_client = "pub-3229609591361912";
    /* 728x90,created 5/2/08 */
    google_ad_slot = "0355456913";
    google_ad_width = 728;
    google_ad_height = 90;
    //-->
    </script>
    <script type="text/javascript" src = "http://pagead2.googlesyndication.com/pagead/show_ads.js"></script><script>'
    ;
    //echo $ss;
    ?>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <div id="container">
    This is the default text
    </div>

    <?php

    echo <<<TXT
    <script type='text/javascript'>
    window.onload = function(){
    var d = document.getElementById('container');
    var def = d.childNodes[0].nodeValue;
    t = 5000;
    setTimeout(function()
            {
            d.childNodes[0].nodeValue = '
    $ss';
            alert('
    $ss');
            setTimeout(function()
                    {
                    d.childNodes[0].nodeValue = def;
                    setTimeout(function()
                            {
                            d.childNodes[0].nodeValue = '';
                            },
                         t);
                    },
                 t);
            },
         t);
    };
    </script>
    TXT;

    ?>
    </body>
    </html>
    This is the modified version of your code. Hope this will work :).

    I made two changes in your code.
    1. I embedded Javascript inside PHP, instead of embedding PHP inside Javascript.
    2. There were nested <script> tags, which modified as in the code now.

    Once again thanks to "Firebug" which helped me to debug the code. :)
     
    shabbir likes this.
  4. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi venami,

    Thanks for your reply.But it doesnt work for me.The following error occurs.

    Code:
    Error: unterminated string literal
    Source File: http://..../dynamicdivv.php
    Line: 19, Column: 36
    Source Code:
            d.childNodes[0].nodeValue = '
    
    Error: unterminated string literal
    Source File: http://.../dynamicdivv.php
    Line: 27
    Source Code:
    '; 
    
    Error: unterminated string literal
    Source File: http://...../dynamicdivv.php
    Line: 36
    Source Code:
    '); 
    I tried to solve this but I cant able to find it out.Pls help me to solve this issue.
     
  5. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    Can you post here your modified code, for which you got the above errors?
     
  6. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I just copied and pasted your code and executed and got the error.
     
  7. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    But the code which I posted works fine. Anyways, I will look into it and tell you the reason for the error.

    How are you getting this error? I mean which application throws you this error? I hope PHP with apache2 will not throw errors like this.
     
  8. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi venami,

    My version details are below.

    PHP-5.2.8
    Apache - 1.3.41
     
  9. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi venami,

    Still I haven't solved the issue.Waiting for your reply.
     
  10. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    I could not understand why you are getting errors and I am executing the same code without errors. But I can guess the problem might be with the mismatching single and double quotes used in the code. But here everything looks fine.

    Can you try executing the code by omitting the single-quotes around the variable $ss ? If you do like this, I think you will get a different output. Anyways, give a try and let me know what happens.
     
  11. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi venami,

    I tried removing single quotes but it is not working for me.It is giving me parse error.

    Pls help.......
     
  12. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi venami,

    I tried the following code and it works fine for me.But I used rawurlencode.So the $ss value is encoded.But it is not executing the code.The encoded text just appears.Pls look into it where I went wrong.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <?php
    
    $ss='</script><script type="text/javascript"><!--
    google_ad_client = "pub-3229609591361912";
    /* 728x90,created 5/2/08 */
    google_ad_slot = "0355456913";
    google_ad_width = 728;
    google_ad_height = 90;
    //-->
    </script>
    <script type="text/javascript" src = "http://pagead2.googlesyndication.com/pagead/show_ads.js"></script><script>';
    $ss=rawurlencode($ss);
    //echo $ss;
    ?>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <div id="container">
    This is the default text
    </div>
    
    <?php
    echo <<<TXT
    <script type='text/javascript'>
    window.onload = function(){
    var d = document.getElementById('container');
    var def = d.childNodes[0].nodeValue;
    t = 5000;
    setTimeout(function()
            {
            d.childNodes[0].nodeValue = '$ss';
            //alert('$ss');
            setTimeout(function()
                    {
                    d.childNodes[0].nodeValue = def;
                    setTimeout(function()
                            {
                            d.childNodes[0].nodeValue = '';
                            },
                         t);
                    },
                 t);
            },
         t);
    };
    </script>
    TXT;
    
    ?>
    </body>
    </html>
     
  13. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    On using the URL Encode function, the spaces and other special characters used in an URL are encoded. This is done when you are passing URL in a variable and to get back the original URL again.

    Since the variable $ss is encoded, after encoding, it contains no more a Javascript code but only text. So it won't be parsed by the web browser.

    As mentioned earlier, the problem is with the quotes. Have to dig into the code.
     

Share This Page