Thompson Thompson - 7 months ago 23
Javascript Question

Include jQuery, if not included already

I want to include the jquery in file, if not included already in the page.
How can I do this?

I wrote this, but it's giving undesired output.

<script type="text/javascript">
if ('undefined' == typeof window.jQuery) {
// jQuery not present
// alert("Jquery Unavailable");
<?php echo '<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"></script>'; ?>
} else {
// jQuery present
alert("Jquery Available");
}
</script>

Answer

you cant do that in PHP. Try this instead

<script type="text/javascript">

if(typeof jQuery == 'undefined'){
        document.write('<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"></'+'script>');
  }

</script>