www.webdeveloper.com
Results 1 to 3 of 3

Thread: How to display loading image

  1. #1
    Join Date
    Jul 2013
    Posts
    31

    How to display loading image

    I have 3 working files :
    index.html :
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     <html>
     <head>
     <script src="urltojquery.js"></script>
     <script type="text/javascript" src="preview.js"></script>
     <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <title>Test Code Preview</title>
     </head>
     <body>
     <textarea id="pastecode" rows="20" cols="50" name="pastecode"></textarea>
     <br /><input type="text" id="language" name="language"/>
     <br /><input type="button" onclick="process()" value="Preview"/>
     <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
     <div id="previewcode"></div>
     </body>
     </html>
    preview.php :
    PHP Code:
     <?php
        
    if(!empty($_POST['pastecode']) && !empty($_POST['language']))
        {
            
    $code=$_POST['pasteode'];
            
    $language=$_POST['language'];
            include(
    "geshi/geshi.php");
            
    $path     '';
            
    $geshi    = new GeSHi($code$language$path);
            
    $geshi->set_overall_style('background-color: #ffffee;'true);
            
    $out $geshi->parse_code();
            echo 
    $out;
        }
        else
        {
            echo 
    "<span style=\"color: red\">Nothing to display</span>";
        }
    ?>
    preview.js :
    Code:
    function process(){
        pastecodeVal = $("#pastecode").val();
        languageVal = $("#language").val();
        $("#previewcode").html("Fetching preview..."+"<hr />")
    
        $.post("preview.php", { pastecode: pastecodeVal, language: languageVal },
                 function(data) {
    
                    $("#previewcode").html(data+"<hr />");
    
              });
    }
    As you can see, when the button is pressed, and preview.php os executing, the message "Fetching preview..." is shown.
    Instead of that, I'd like to display a loading image when preview.php is getting executed. I've got an image loader.gif in the same folder as the three files. How to do it?

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    Insert a img tag (with the src="loading.gif" and an id="ldgImg") in your HTML page with a CSS style display:none to hide it (an image is by default a inline élément).
    Then show this image at the beginning of the function with something like this (not tested):
    Code:
    document.getElementById('ldgImg').style.display='inline'; // Or 'block' it depends on your CSS choïce
    Finally mask this image with the Ajax call back response
    Code:
    function(data) {
            $("#previewcode").html(data+"<hr />");
            document.getElementById('ldgImg').style.display='none';
    });
    NB : Use more CSS (margin or padding are used to remove this disgracious <br><br>...)
    Last edited by 007Julien; 07-06-2013 at 07:38 AM.

  3. #3
    Join Date
    Jul 2013
    Posts
    31
    Quote Originally Posted by 007Julien View Post
    Insert a img tag (with the src="loading.gif" and an id="ldgImg") in your HTML page with a CSS style display:none to hide it (an image is by default a inline élément).
    Then show this image at the beginning of the function with something like this (not tested):
    Code:
    document.getElementById('ldgImg').style.display='inline'; // Or 'block' it depends on your CSS choïce
    Finally mask this image with the Ajax call back response
    Code:
    function(data) {
            $("#previewcode").html(data+"<hr />");
            document.getElementById('ldgImg').style.display='none';
    });
    NB : Use more CSS (margin or padding are used to remove this disgracious <br><br>...)
    OMG!!!! Its this simple????!!!!
    Thanks!! Its working fine!!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles