Results 1 to 9 of 9

Thread: Dynamically update div content

  1. #1
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Dynamically update div content

    Hi

    General Q - and forgive me if this is in the wrong area. I have no coding experience but want to dynically update div content, changing text and background images (styled with css). This is to update an area on a page used for promotions and I want a slideshow effect but the promotions are html and css styled so it is not just a case of having a slideshow for images (if that makes sense).... Is there a way to do it and where should I be looking, I want to avoid flash slideshows if possible?

    Thanks in advance for any help.
    Last edited by roadjan; 03-31-2009 at 01:14 PM.

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    You can do this with JavaScript without much trouble. DD provides some slideshows here. Select the one you want to use. Read the steps required to integrate it in your page. If you have any issue, post the issue in the forums, so that the users will be able to help you.

  3. #3
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    These are good, but i want up date the text within the div's dynamically, so after, say 5 seconds, the text changes in the div from "original text" to "new text" and then possibly "newer text" and then back to "original text"?

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Yes you can here is a simple version of the feature that you've mentioned in your post

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <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>
            <a href="#" id="link">Click to change</a>
            <script type="text/javascript">
                var a = document.getElementById('link');
                a.onclick = function(){
                    var d = document.getElementById('container');
                    var def = d.childNodes[0].nodeValue;
                    t = 5000;
                    setTimeout(function(){
                        d.childNodes[0].nodeValue = "New Text";
                        setTimeout(function(){
                            d.childNodes[0].nodeValue = "Newer Text";
                            setTimeout(function(){
                                d.childNodes[0].nodeValue = def;
                            }, t);
                        }, t);
                    }, t);
                }
            </script>
        </body>
    </html>
    Copy the above furnished source code and save it with an extension of either .htm or .html. After this open the HTML file in any browser and click the link 'Click to change'. After click the link it will change the content of the div element after 5 seconds then after 5 seconds it will again change the content and after 5 seconds it will change the content back to the original content.

    Hope this helps.

  5. #5
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    This is great - one final query, can you do it without having to click on a link, i.e. it is automated when the page is loaded?

    Thanks for all your help

  6. #6
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Sure check the following source code. Just load the page and wait the updation will be done automatically unlike the earlier time.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <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 = "New Text";
                        setTimeout(function(){
                            d.childNodes[0].nodeValue = "Newer Text";
                            setTimeout(function(){
                                d.childNodes[0].nodeValue = def;
                            }, t);
                        }, t);
                    }, t);
                };
            </script>
        </body>
    </html>
    Hope this helps.

  7. #7
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Brilliant!! I have the "javascript made easy" book from sitepoint on it's way as we speak!! Is there a way to loop this so it is continuous? I think I am there after that.

  8. #8
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Here it is

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <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">
                /**
                 * @method update
                 * The function responsible for updating a div element starting from the page loading to the page close.
                 * @param {void}
                 * @return {void}
                 */
                function update(){
                    var d = document.getElementById('container');
                    var def = d.childNodes[0].nodeValue;
                    t = 5000;
                    setTimeout(function(){
                        d.childNodes[0].nodeValue = "New Text";
                        setTimeout(function(){
                            d.childNodes[0].nodeValue = "Newer Text";
                            setTimeout(function(){
                                d.childNodes[0].nodeValue = def;
                                update();
                            }, t);
                        }, t);
                    }, t);
                }
                
                /**
                 * Inovking the update function through window object's onload event.
                 */
                window.onload = update;
            </script>
        </body>
    </html>
    Hope this helps.

  9. The Following User Says Thank You to codeexploiter For This Useful Post:

    roadjan (03-31-2009)

  10. #9
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi I am wondering how do i make this script show HTML?
    As it seems it shows html as text.

    Can anyone help?
    The web in one word.

Bookmarks

Posting Permissions

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