Given a document, The task is to determine whether JQuery has been loaded or not, If not then load it dynamically using JavaScript. Below are the examples to check:
Approach:
- First, check if it is loaded.
- If not loaded then load it dynamically, create a script element and add properties like(type, src) to it and then finally append it at the end, inside of the head element.
Example 1: In this example, the JQuery is already loaded so the message ‘Script already loaded!’ prints on the screen.
html
<script src=
</script>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<p id="GFG_UP">
</p>
<button onClick="GFG_Fun()">
click here
</button>
<p id="GFG_DOWN">
</p>
<script>
var up =
document.getElementById('GFG_UP');
var down =
document.getElementById('GFG_DOWN');
up.innerHTML =
"Click on button to check whether JQuery is loaded";
function GFG_Fun() {
if (!window.jQuery) {
var el = document.createElement('script');
el.type = "text/javascript";
el.src =
document.getElementsByTagName(
'head')[0].appendChild(el);
down.innerHTML = "Script loaded successfully!";
} else {
down.innerHTML = "Script already loaded!";
}
}
</script>
|
Output:

Check if a jquery has been loaded or not
Example 2: The src attribute of script element is changed to sc. In this example, the JQuery is not loaded so message ‘Script loaded successfully!’ prints on screen after successful loading.
html
<script src=
</script>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<p id="GFG_UP">
</p>
<button onClick="GFG_Fun()">
click here
</button>
<p id="GFG_DOWN">
</p>
<script>
var up =
document.getElementById('GFG_UP');
var down =
document.getElementById('GFG_DOWN');
up.innerHTML =
"Click on button to check whether JQuery is loaded";
function GFG_Fun() {
if (!window.jQuery) {
var el =
document.createElement('script');
el.type = "text/javascript";
el.src =
document.getElementsByTagName(
'head')[0].appendChild(el);
down.innerHTML = "Script loaded successfully!";
} else {
down.innerHTML = "Script already loaded!";
}
}
</script>
|
Output:

Check if a jquery has been loaded or not
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
16 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...