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 8182295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:49:03+00:00 2026-06-07T00:49:03+00:00

I have got this code: function init(){ if (typeof window.jQuery !== ‘function’) { var

  • 0

I have got this code:

function init(){
    if (typeof window.jQuery !== 'function') {
        var link = document.createElement('script');
        link.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
        document.getElementsByTagName('head')[0].appendChild(link);
    }
}

window.onload = init;

 if (typeof window.jQuery === 'function')  {
    $(document).ready(function(){
        alert(1);
    });
}

What I am trying to do is to add jquery script link to the head if jQuery doesnt exist and then run the code. What could be best is to check whether jquery exists in the head as soon as possible and then add a link to the source. But I dont know how to achieve so?

UPDATE:
An alternative approach would be using a function:

  function init(){
   if (typeof window.jQuery !== 'function') {
      var link = document.createElement('script');
      link.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
      document.getElementsByTagName('head')[0].appendChild(link);
      start_code();
   }
   else{
       start_code();
   }
}  
window.onload = init;
function start_code(){
           $(document).ready(function(){
                alert(1);
           });
     }

I could basically call a function after everything is loaded.. But this doesnt work well. cause the $ is not defined error is being thrown too

  • 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-07T00:49:05+00:00Added an answer on June 7, 2026 at 12:49 am
    function init(){
       if (typeof window.jQuery !== 'function') {
          var link = document.createElement('script');
          link.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
          document.getElementsByTagName('head')[0].appendChild(link);
       }
    }
    

    or use the HTML5 boilerplate approach

    <script>window.jQuery || document.write('<script src="/js/vendor/jquery-1.7.2.min.js"><\/script>')</script>
    

    placing this script at the bottom of the page (for performance purpose). Then, if you need to use jQuery function before, just take a look at this resource: stop paying your jQuery tax

    since document.write here is creating a script block (which is synchronous) all you have to do is simply

    <script>window.jQuery || document.write('<script src="/js/vendor/jquery-1.7.2.min.js"><\/script>')</script>
    <script>
        $(document).ready(function(){
            alert(1);
        });
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got code like this var challegneListener; $(document).ready(function(){ var challegneListener = setInterval(challengeListenerBot(),5000); });
Code is not running on .click when I have this: $(.cancel).click(function() { alert(got here);
I have got this code: $subject=<<<EOD <object height=400 width=500><param name=allowfullscreen value=false> <param name=AllowScriptAccess value=always>
I have got this code for detection of mobile device. <?xml version=1.0 encoding=UTF-8?> <configuration>
I have got this code: XDocument xdoc = XDocument.Load(URI); XElement root = xdoc.Element(forecast); //get
I have got this code and I am trying to understand the convention followed,
ok i got this problem. i have this routes: (code bit change) File:/home/dotcloud/current/config/routes.js exports.routes
I have got some javascript code and I'd like to convert this to C#.
I have got a piece of code, that should countdown some number (in this
I got this code: GrooveOnDownload *dlg = [[GrooveOnDownload alloc] init]; NSURLDownload *dw = [[NSURLDownload

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.