Sunday, March 31, 2013

jQuery getScript, FireBug And Caching

Recently in one of my projects I had a requirement of loading JavaScript files dynamically. It was then I took a look at the jQuery getScript method. At first it looked perfect for my requirement, however as I started using it for my project I realized there were two drawbacks with its default implementation.

  1. There isn't any caching for the scripts loaded by getScript
  2. Debugging these scripts in FireBug is quite difficult since FireBug cannot map the script code to the script filename

In this post, I am going emphasize on my experiences with getScript and how I got both it to work with both these requirements. I am sure people who are spending time to get these features working will find this useful.

To help me with this, I put up an example Visual Studio project available here. I used Visual Studio for comfort but any text editor will work since it is all about HTML and JavaScript.

Basic Example

Well, getScript is pretty easy to use. In its simplest form, all we have to do is provide it with the script name. Here's an example on how it looks.

But the interesting point is the way it works. Here's a screenshot in FireBug which show three calls being made to the server since getScript appends a random number to the server call causing the browser cache to fail. And the script debugger is also unable to recognize the script name.

Enabling Caching

Solving the caching issue was simple. Following the recommendation on the jQuery getScript documentation page, I setup AJAX caching using ajaxSetup as show below.

This worked well but the only problem that I noticed was that ajaxSetup was a global option and setting it there would make all AJAX calls cached which could mean trouble for the regular data calls we make. Fortunately the documentation page also had a solution for this. I wrote a new jQuery method using that solution as shown below.

Making getScript FireBug Compatible

At first I had had no clue on why this was happening. But thanks to the guys at TechMonks who wrote an absolutely brilliant article on this problem, I could get this fixed. In short the problem was because of how jQuery implements getScript wherein it uses AJAX to the script content and makes it inline. Do read the entire article here for more details.

Here is a code snippet which make getScript compatible with FireBug. The reason why this works is because it loads the JavaScripts files as external resources instead of making it inline and FireBug shows them in the debugging panel.

Internal Caching

The new firebugGetScript from TechMonks works great with FireBug and also facilitates browser caching. However I had one more issue with it. My web application was completely dynamic and had to load a lot of scripts on the fly. Because of this there were scenarios where I was calling the same script through firebugGetScript. However the method wasn't smart enough to recognize this and added the script element multiple times.

I added the ability to recognize this and changed the code to the following.

And things looked perfect with this. I have been using this for quite some time in two different web applications and it's working as expected.

That's it for this post, hope the next time isn't as far away as this ;)

43 comments:

ruby on rails developer said...

Valuable post regarding jquery scripting. I love to read your blog because which is pond of knowledge.

Anonymous said...

Thank you for a great article, it clarified a lot of things for me

Unknown said...

Hi, Nice post in jquery to getscript,FireBug and caching

-Hari
Theosoft

Unknown said...

SQIAR http://www.sqiar.com/services/data-warehousing/ is a leading Business Intelligence company and provides Data Warehousing Services in United Kingdom and USA.

Reset hotmail password said...

thanks, your post very Nice and Great. It is very helpful for me. I wish you to check Reset Hotmail password

friv4 said...

This article is really fantastic and thanks for sharing the valuable post.
games2girls 2

juegos de frozen said...

Hello, just wanted to say, I loved this article. It was practical.
Signature:
download descargar facebook gratis para Android celular and download free descargar facebook apk en español, descargar facebook plus , descarga facebook

Unknown said...

Posts shared useful information and meaningful life, I'm glad to be reading this article and hope to soon learn the next article. thank you
Signature:
download free descarga Whatsapp and download whatsapp baixar online and descargar whatsapp , baixar whatsapp

baixar facebook movel said...

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write
Signature:
Jugar juegos de frozen en línea gratis, los nuevos de princesa de Disney juegos frozen - la princesa encantadora y linda. Divertirse frozen!

Unknown said...

Thanks for sharing the information. It is very useful for my future. keep sharing
Signature:
facebook iniciar sesion gratis - Sitio Oficial iniciado sesión en Facebook lengua española. facebook inicio sesion rápido, facebook iniciar sesion en tu cuenta de entrar facebook

Unknown said...

This is also a very good post which I really enjoyed reading. It is not everyday that I have the possibility to see something like this.
Signature:
download baixar facebook movel, Facebook movel, baixar facebook. Facebook chat, baixar whatsapp, fazer o download baixar whatsapp gratis para Android, iPhone. Últimas Facebook

Unknown said...

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write
Signature:
The place to play all unblocked games at school online. Here you can find every blocked games such as: unblockedgames , unblocked games happy , unblocked games 77 , Gmod Free

Unknown said...

Today, I read post.... thank u for sharing.
Thanks u so much.
run 3 game
football heads
juegos de terror online
mahjong games
Cool math 4 kids

Unknown said...

I found a lot of information here to create this actually best for all newbie here. Thank you for this information.
Signature:
i like play games play happy wheels online and play happy wheels unblocked full and Download retrica online includes more than eighty different filters with many different styles and include retrica indir , and zombie tsunami is the ideal game for anyone who loves the running game genre

Unknown said...

An exceptionally instructive site. The way you have offer this data is truly extremely grateful. Would like to see more on this point here. A debt of gratitude is in order for posting this data here.
top b tech colleges in india

navya said...

I have read your blog its very attractive and impressive. I like it your blog.

.Net Training in Chennai | .Net Online Training | Dot Net Training in Chennai

Dot Net Online Training | LINQ Online Training

Unknown said...

Learning new technolgy would help oneself at hard part of their career. And staying updated is the only way to survive in current position. Your content tells the same. Thanks for sharing this information in here. Keep blogging like this.
iOS App Development Company in India

Unknown said...

wow its great thing and wish to you do more level of things and i am waiting to get your new ideas. keep share more things.
IELTS Coaching in Chennai

Drag Racer V3 said...

That's so nice. Thanks! dragracerv3game.com

Unknown said...

It is really very excellent. Because of all given information was wonderful and it's very helpful for me. Thanks for sharing
SAP Training in Chennai
SAP ABAP Training in Chennai
SAP FICO Training in Chennai
SAP MM Training in Chennai

Unknown said...

Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....

Solar Rooftop
Solar Water Heater
Solar Panel
Solar Module
Energy Efficient
BLDC Fan
Solar Power
Power Plant
Solar Training
Solar Pump

akash thakur said...

hi, nice information is given in this blog. Thanks for sharing this type of information, it is so useful for me. nice work keep it up.Laminated Pine Doors Manufacturers

ajay prakash said...

Very nice blog, Thank you for providing good information.
best aviation academy in chennai
cabin crew training institute in chennai
Airline Courses in Chennai
airport ground staff training in chennai
medical coding training
fashion designing courses in chennai part time
interior design courses in chennai part time

Aruna Ram said...

Wonderful post!!! These ideas are very nice and worthful content. I feel very good to read your great post and Thanks for your brief explanation. Well done and good job. I like more updates to your blog...
Hadoop Admin Training in Chennai
Hadoop Administration Training in Chennai
Primavera Training in Chennai
Xamarin Training in Chennai
Social Media Marketing Courses in Chennai
Hadoop Admin Training in Adyar
Hadoop Admin Training in Velachery
Hadoop Admin Training in Tambaram

nadiya said...

This is an awesome post.Really very informative and creative contents.
IELTS Coaching in Chennai
Best IELTS Coaching in Chennai
French Language Classes in Chennai
pearson vue exam centers in chennai
Informatica MDM Training in Chennai
Hadoop Admin Training in Chennai
IELTS Coaching in Tambaram
IELTS Coaching in Anna Nagar

DedicatedHosting4u said...

Thanks for actuation this along. This info can facilitate Pine Tree State to be more practical. Enjoyed reading the article higher than , very explains everything intimately. Thanks and sensible luck for the forthcoming articles

DedicatedHosting4u.com

RoyceRobbie said...

Good information and, keep sharing like this.

Crm Software Development Company in Chennai
web portal development company in chennai
web portal development services in chennai
professional web design company in chennai
smo company in chennai
seo company in chennai
best seo company in chennai
erp software development company in chennai
sem services in chennai
twitter marketing company in chennai

sahil sharma said...

It is very important to choose the right level of training. The HIPAA privacy officer and HIPAA security officer CANNOT take basic level training (2 hours). They need at least 20+ hours of training to have a clear understanding of privacy and security rule requirements. The basic reason for the non-compliance is that nobody in the company understands the complete requirement of rules. As a result, they are unaware of policy and procedure requirements, contingency plan, risk analysis, and audit. HIPAA compliance training will provide knowledge to a compliance officer to make company HIPAA compliant.

jeewangarg said...

Wow!!! It was really an Informational Article which provide me with much Insightful Information. I would like to first thank the author for such an Insightful Content. If someone here is looking for a Website Designing Company in India, then look no further than Jeewangarg.com as we are the Best website designing in Delhi working in the arena for the last 8 years.
website designing company in NCR
website designing company in India

Chanson Water said...

Great website, continue the good work! Also visit my site – alkaline water machine

rightselects said...

Thanks for posting such a great post . Right Selects provides its users with the exact information of the “top 10” or “top 5” industry leader of the particular industry. For instance, if you are looking for selecting the best among the array of water purifiers in India, you can visit us for getting the most accurate information about “Top 10 water purifier for home”. Additionally, at right selects, you can have the information on lifestyle products, electronics, appliances, water purifiers, restaurants as well as about the best travel places.

Daily Image Funda said...

It looks too much informative for me..

Check out these images
Best motivational quotes in Hindi
Good Morning Images for friday
Good Morning Images with flowers HD
Good Night Romantic Images
Best happy weekend images
Navratri Good Morning Images

Unknown said...

best ent doctors in chennai
We are one of India's largest private ENT hospitals located all over Chennai. Our core services – are the treatment of ailments of Ear, Nose and Throat.

Online Front said...

Mostly I use to wait for informative article on daily bases to get something new, but today i found your blog very interesting and unique, providing the information helpful to others. Keep it up and waiting for your new updates thanks. We offer multiple services in digital marketing, some of our services are:

Digital marketing Company in Delhi
SMM Services
PPC Services in Delhi
Website Design & Development Packages
SEO Services Packages
Local SEO services
E-mail marketing services
YouTube plans
Digital Marketing Service in Delhi

Diya Kaur said...

Thanks for provide great informatic and looking beautiful blog, really nice required information & the things i never imagined and i would request, wright more blog and blog post like that for us. Thanks you once agian
collage call girl service in darbhanga
collage call girl service in bhagalpur
collage call girl service in patna
collage call girl service in gaya
collage call girl service in bijnor

Rahul Sharma said...

Nice Post. You are Share great post and keep posting.
Diabetes Management in Meerut
Google 360 Virtual Tour Services
top 10 cbse schools in meerut
Website Designer
Meerut Boy
SEO Solution India

raj said...

wonderful article..keep up
Solidworks project in Coimbatore | solidwork Training in coimbatore | Mechanical Project in Coimbatore | Mechanical Cad software Training in Coimbatore | Civil Autocad Course in coimbatore | Revit Course fees in coimbatore | revit training Institute in coimbatore | Cad layout design in coimbatore | Civil Course in coimbatore | Design Project In coimbatore | AutoCad Centre in Coimbatore | Cadd Cetre in Coimbatore | Solidworks in coimbatore | Best Cad Centre in Coimbatore

GABACKPACK said...

Excellent work I Really impressed and got lots of information from your post and encourage me to work as best as i can. keep it!
Quick Heal Total Security Crack
My Notes Keeper Crack
PilotEdit Crack
Quick Heal Total Security Crack
RescuePRO Deluxe Crack

Tamil Typing said...

Thanks for sharing informative post. Are looking for best Tamil typing tool online, make use of our Tamil typing software to make translation faster. Thirumana Porutham in Tamil | Samacheer Kalvi Books PDF

brijesh kumar said...

designing high-quality shower and warming lights. Explore our collection of quality and reliable led shower lights & more!

Gerry Max said...

How to Unlock Yahoo Email?

Any malfunction with Yahoo's products may cause your account to be suspended. Yahoo is very strict about maintaining the quality of its products and services. The Sign-in Helper can be used immediately to regain access to your Yahoo account if it has been locked temporarily. To Unlock Yahoo Email, use the Yahoo Help Forum Page.

Gerry Max said...

How to Setup Optimum Mail Settings on Android?

To manually set up an optimum.net email account on your Android phone, you can start by selecting the icon of 'Mail' from the applications screen and then enter your incoming (IMAP) and outgoing (SMTP) server information. Then, enter the email address and email password. Now, choose the option of 'Manual Setup' and then choose Optimum IMAP settings. Now, make sure to enter the IMAP server details and port.

Gerry Max said...

Why Facebook is Not Responding Today?

People often encounter the problem of why Facebook not working while using the social network. If you are wondering why Facebook not working, then the cause could be various factors, including Facebook caches, Facebook account errors, phone storage, internet connection, etc. So, to solve this issue, try refreshing your browser or reopening the site; on a mobile app, close and reopen the app. You can also check if Facebook is offline and then restart your device, update apps, and clear your cache.