• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Software and Hardware
  • New and Happening
You are here: Home / Programming and Scripting / JavaScript: Overcome Slow Loading JavaScript On A Web Page

JavaScript: Overcome Slow Loading JavaScript On A Web Page

November 19, 2009 by Sze Hau 2 Comments

Sometime a web page may load slowly due to loading a third party JavaScript source file from other domain. Most of the time, a web site comes with advertisement scripts or scripts for keep tracking visitors and these scripts are normally provided by third parties like Google Adsense, Statcounter, Amazon and etc. When these scripts’ loading speed is extremely slow, it blocks the whole page from loading.

One of the way to overcome the slow loading of JavaScript problems is to move the code that load the JavaScript file to the end of the website before the body tag. For example, <script type='text/javascript' src='http://www.example.com/some_script.js'></script>. This is so that the browser can load and render the whole page and then only load and run the script.

But this does not actually solve most of the problem. This is because in most time, in most JavaScript, the programmers will use the document.write() method to render the HTML code at the position where the JavaScript is loaded. For example, if you move the source line that load a advertisement to the bottom of the page and left the parameters on top, the advertisement will be loaded at the bottom of the page.

To solve this problem, we can implement a simple tricks with little JavaScript code. The idea is like this:

  • Put a empty div place holder at the place where you originally want to put and give it a unique name, let’s say ads1_ori.
  • Put a hidden div place holder at the bottom, put the scripts that load slowly inside the place hold and give it a unique name, let’s say ads1_new.
  • At bottom of the page, right before the body tag, write a few line of JavaScript code to load the HTML content from ads1_ori to ads1_new.

Example of implementation:

<html>
  <head><title>Some Web Site</title></head>
  </body>

  ....

  <div id="ads1_ori "></div>

  ...

  <div id="ads1_new" style="display:none">
    <!-- advertisement -->
    <script type="text/javascript">
      paramenter = "abcdefg";
    </script>
    <script type="text/javascript" src="http://www.example.com/some_script.js"></script>
    <!-- advertisement -->
  </div>

  <script type="text/javascript">
    document.getElementById('ads1_ori').innerHTML = document.getElementById('ads1_new').innerHTML;
  </script>
  </body>
</html>

More from my site

  • Javascript: Get Query String and Key-Value Pairs from URLJavascript: Get Query String and Key-Value Pairs from URL
  • Adobe Acrobat Reader: Possible Security Vulnerability When Acrobat JavaScript Is EnabledAdobe Acrobat Reader: Possible Security Vulnerability When Acrobat JavaScript Is Enabled
  • Earn Money: NeoBux – A Get Paid to Click ProgramEarn Money: NeoBux – A Get Paid to Click Program
  • How to Add Advertisement Code to WordPress Theme’s SidebarHow to Add Advertisement Code to WordPress Theme’s Sidebar
  • Magpie: An Ad Network for TwitterMagpie: An Ad Network for Twitter
  • Short URL: Top 5 Websites That Provide Free Short URL ServiceShort URL: Top 5 Websites That Provide Free Short URL Service

Filed Under: Programming and Scripting, Tips and Tricks Tagged With: JavaScript

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Comments

  1. jez says

    November 30, 2009 at 5:59 am

    while the concept sounds amazing it does not work for me. I tried this idea with google adsense and the ad would just not show up at all. any idea? thanks

  2. szehau says

    December 6, 2009 at 12:42 pm

    Hi jez,

    I have no problem using the code for Google Adsense. Maybe you have some coding error in your JavaScript?

Leave a Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Software and Hardware

MD5 and SHA1 Checksum Using Windows

July 5, 2017 By Sze Hau Leave a Comment

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in