Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9223733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:08:33+00:00 2026-06-18T04:08:33+00:00

I’m trying to prettify my code in Blogger. I’ve linked Google JS and CSS

  • 0

I’m trying to prettify my code in Blogger. I’ve linked Google JS and CSS files to my template. The problem is that I want the code to be prettified on page load, so I add prettyPrint(); to page load event on the template.

<body onload="prettyPrint();">

This code doesn’t get executed. However, if I type prettyPrint() manually in the console, my codes get prettified correctly. Does blogger template block invoking JS functions manually?

EDIT I get it to work by manually invoking the function in every post that I needs code prettifying (see below). Still, I want to know why I can’t do it on the template.

<pre class="prettyprint linenums lang-js">
function testCode(){

}
</pre>
// I have to do this in every post :-s
<script type="text/javascript">
prettyPrint();
</script>

EDIT 2 The README said that I shouldn’t use prettyPrint() directly as a handler but wrap it in a closure instead. So I added this code, similar to the example in the README, in my <head> but to no avail.

<script type='text/javascript'>
window.addEventListener('load', function (event) { prettyPrint() }, false); 
</script>

OR

<script type='text/javascript'>
document.addEventListener('DOMContentLoaded',function() {
    prettyPrint();
});
</script>

EDIT 3 My template HTML is just the default Dynamics View (Classic) Template with the prettify library added as explained above.

EDIT 4 Here is a link to demonstrate the problem: http://testprettyprint.blogspot.com/2013/02/blog-post.html — the code block is not prettified automatically but if you open Chrome’s console and type prettyPrint() the codes will be correctly highlighted.

EDIT 5 The reason why I think it’s my problem not blogger’s because this guy still has his code prettified using the same technique: http://errorbuster.blogspot.com/2012/07/prettify-syntax-highlighter-for-blogger.html

EDIT 6 As Jeffery To has pointed in his answer, the Dynamics View loads blog content with AJAX, so any JS call on document load will get executed BEFORE the actual content is loaded. Therefore, any JavaScript performed on the actual blog content, not the document, is invalid. So I guess the question now is how to hook into Dynamics View ajax:complete event, if there is such a thing, but I doubt there is. Thanks everyone who has replied. I’m not sure if this can be counted as a bug, but I’ll file an issue with blogger.

CONCLUSION Please read Jeffery To’s answer. He found the event to invoke the function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:08:34+00:00Added an answer on June 18, 2026 at 4:08 am

    I believe your test page is using Blogger’s “Ajax templates” (not sure what the official name is), whereas the other link you posted is using classic templates.

    If you reload your test page, you’ll first see the Blogger logo in the middle of the page, then your content appears. If you view the test page source, you’ll see a lot of code, but not your content. I believe with layout templates, Blogger’s code first runs, then it fetches your content using Ajax. Any JavaScript code that runs on document ready (DOMContentLoaded) or window load will run before your content is loaded into the page.

    I believe if you add a HTML/Javascript widget to the page (after your content block), then include a call to prettyPrint() within that widget, it should be called every time a post is displayed.


    Update: The official name of this template is “Dynamic Views”, and it appears that the HTML / JavaScript widget isn’t supported for Dynamic Views. I can’t find any mention of an API that lets you hook into Blogger’s JavaScript, so I believe the answer (for now at least) is that it’s not possible to add template-level code to do what you want. The most practical way would be to include a script tag with prettyPrint() in every post 🙁


    Update 2: I’ve traced through Blogger’s code and I think I’ve found a suitable (and bindable) event. Try including this after the Dynamic Views code (in the head element, after the <script src='//www.blogblog.com/dynamicviews/...'></script> tags):

    <script>
    $(window.blogger.ui()).on('viewitem', function (event, post, element) {
        prettyPrint();
    });
    </script>
    

    The viewitem event is triggered every time the user views an item (which can happen multiple times in the page’s lifetime). The element argument is a jQuery object of the item’s container element, so if you want to save prettyPrint() a little time you can do this:

    <script>
    $(window.blogger.ui()).on('viewitem', function (event, post, element) {
        element.each(function () {
            // first argument is a callback function
            // second argument is a root element or document to check
            prettyPrint(null, this);
        });
    });
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.