SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    prettyPrint() only has an effect once (google-prettify).

    I have this JavaScript (with jQuery):

    Code:
    var g_files_added, socket, cookie, database = null;
    var file_contents = [];
    function viewFile(key, filename) {
        $('#title-filename').text(filename);
        $('.prettyprint').text(file_contents[key]);
        $('#viewFileModal').modal('show');
    }
    $(document).ready(function() {
        $(document).on('shown', '#viewFileModal', function(event) {
                prettyPrint();
        });
    });
    
    // Variables have been set in code not shown, irrelevant to problem.
    // prettyPrint() is called everytime the #viewFileModal is shown,
    // but its effect is only felt once.
    So prettyPrint() is invoked every time the viewFileModal modal box (courtesy of Bootstrap) is shown, it's just that it only seems to have an effect once per page load.

    I have tried commenting out prettyPrint() and entering at the JS console after making the modal box appear. It indeed only has an effect the first time the box is shown (per page load).

    Any ideas? I have been stuck on this a while. I also tried putting the call to prettyPrint() in the viewFile function; but the effect is the same.

    Thanks a lot.

    Sam.

  2. #2
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Solution was to change to be like this. That is, add whole pre dynamically rather than just text. Before I was only adding text to an existing pre block.

    Code:
    var g_files_added, socket, cookie, database = null;
    var file_contents = [];
    function viewFile(key, filename) {
        $('#title-filename').text(filename);
        $('#fileblock').html('<pre class="prettyprint">' + file_contents[key] + '</pre>'); // fileblock is a div.
        $('#viewFileModal').modal('show');
    }
    $(document).ready(function() {
        $(document).on('shown', '#viewFileModal', function(event) {
            prettyPrint();
        });
    });


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
  •