0

I am trying to load JQuery library from a JavaScript file and calling a function that is using JQuery.

JS1.js

$(document).ready(function () {

    //var id = 728;
    (function () {
        var jq = document.createElement('script'); jq.type = 'text/javascript';
        jq.src = '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(jq);
    })();
    ShowData('test');
});

Markup:

<head runat="server">
    <title></title>
    <script src="JS/js1.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="embedloc">

    </div>
    </form>
</body>

But its not working. The function is still unable to find the JQuery. When I am adding it manually on the HTML page, it runs successfully. Please help.

1
  • You need to load the Jquery Library before you can use it. Put a script tag to the Jquery version you are using or to the CDN that you are getting Jquery from. Make sure that it is placed before your script that way when your script executes it will have knowledge of the $ sign. Commented Jan 10, 2014 at 15:32

2 Answers 2

1

You're trying to use jQuery before you load it.
That's not going to work.

If you want to load a script dynamically, you can only use it after the <script> tag has asynchronously finished loading.

Sign up to request clarification or add additional context in comments.

Comments

0

A solution:

<head>
   <script>
        document.write('<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript">  </' + 'script>');
   </script>
</head>

(Sencha Touch framework uses this technique to load scripts).

Hope this helps. Cheers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.