How to check for the existence of a function before I call it. Trying to avoid the object not found error message.

I am using some JQuery functions that are accessible on the web. For example : ajax.aspnetcdn.com

My web application runs on an Intranet. However I am accessing the plugins that are located on the Internet.
Everything works great, however if the website becomes disconnected from the Internet, the JQuery functions at aspnetcdn.com will not be found. In that event an exception will be thrown in my call to the JQuery code, and I will get a "object not found" error message for the function being called. So using JQuery or Java Script how can I check if the function in not defined before attempting to call it?
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
you would have to check first if jQuery has been loaded/initialized. Then you have to check each plugin when you load them from external systems.

Sample for jQuery library
<script src="//ajax.aspnetcdn.com/ajax/jquery/jquery-2.0.0.min.js"></script>
<script>window.jQuery || document.write('<script src="js/jquery-2.0.0.min.js">\x3C/script>')</script>

Open in new window


Sample for a plugin providing the foo method:
<script>
if (typeof jQuery.foo == 'undefined') {
    document.write(unescape("%3Cscript src='/js/jquery.foo.plugin.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>

Open in new window


If you use a lot of plugins you might want to think about using Require.JS:
https://github.com/jrburke/requirejs/

HTH
Rainer

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TEKDesignCEO/Consultant/OwnerCommented:
Best way to solve this is make a local copy of the JQuery framework library on the local website so they are not affected by the disconnection to the internet and the unavailability of the JQuery on aspnetcdn.net. This will also reduce your internet bandwidth usage as JQuery will not need to be downloaded from the internet every time the website is visited and used.

If your reason for using the CDN is to always make sure you have the latest version of JQuery simply write a script that checks for updated version of JQuery on interval from the aspnetcdn.net site and download and replaces the local one.
brgdotnetcontractorAuthor Commented:
My longterm plan is to just download the cdn code locally. For now though I need more help on how to catch the exception so that I do not get the error message "Error: jQuery' is undefined. I am not passing in a variable into my function, and so I don't think
if (typeof jQuery.foo == 'undefined') {
will work in this situation. Here is my specific code. below. The exception will be thrown at line 2 below. Can someone help me out some more so that I can prevent the JQuery is undefined message from being displayed.

<script src=”http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.js”  type=”text/javascript”></script>

<script src=”http://ajax.aspnetcdn.com/ajax/jquery.ui.js”  type=”text/javascript”></script>
<script src=”http://ajax.aspnetcdn.com/ajax/jjquery.ui/1.8.16/themes/humanity/jquery-ui.css.js”    rel=”stylesheet”  type=”text/css” />

1  <script type=”text/javascript”>
2  $(function() {
3     $(“[id$=myTetBox]”).datepicker({
4   changeMonth: true,
5   changeYear: true
6});
7});
</script>
Rainer JeschorCommented:
Hi,
the "$" is just the shortcut for jQuery therefore you have to check before that line of code:
if (typeof jQuery == 'undefined') {
  // jQuery not loaded
}

Open in new window


To ensure that jQuery is loaded you might want to include this line after your initial reference to the CDN jQuery file:
<script>window.jQuery || document.write('<script src="js/jquery-1.7.2.min.js">\x3C/script>')</script>

Open in new window

and of course you should have that file also hosted on your web server.

For jQuery UI you can check this:
if (typeof jQuery.ui != 'undefined') {
  // jQuery UI loaded
}

Open in new window


HTH
Rainer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.