1

I am new to jquery and would like to use some jquery code for my project. I want to use jquery from the Google CDN in my html file, but I already have a linked external javascript file to it and I don't know how to incorporate the library. Also, when I want to use code later, do I write it inside the external javascript file or inbetween <script> </script> tags in the html document?

Currently my HTML looks like this:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<script type="text/javascript" src="onlineshop.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
0

2 Answers 2

4

As long as you reference the JQuery library before your external JavaScript file, you don't have to do anything. Just use $ as normal. Reverse the order of your two <script> references.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="onlineshop.js"></script>

And, to be clear, these references do NOT have to be in the <head> section of your document. Often, it's a good idea to place them just before the close of the body of the web page (</body>), so that the browser can load and parse the HTML content uninterrupted by having to download and parse JavaScript files. This can give your users the appearance of a faster loading page because the UI can appear and become responsive faster than if the scripts were at the top of the page.

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

Comments

3

Since you'll be using Jquery in your external javascript file. Include the Jquery CDN before it.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>    
<script type="text/javascript" src="onlineshop.js"></script>

This will work fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.