jquery loading after script that is included below it - javascript

I have a page where I have something like this at the end of the page:
<script src="jquery.js"></script>
<script src="onload-test.js"></script>
</body>
</html>
In my network tab it shows everything being loaded using h2 (http/2) and onload-test.js is loaded before jquery.js causing an error
"Uncaught ReferenceError: $ is not defined"
as I am using $(document).ready() in my script. The network tab shows jquery.js being queued for ~200ms.
Under what circumstances can this happen? and how can it be prevented?

use asycn while loading
<script async src="jquery.js"></script>
<script async src="onload-test.js"></script>

You can add jquery reference inside "<head></head>" section for best practice.
Body must contain only body specific js references.
jquery is a kind of global library which should be loaded in the <head></head> section.
You can refer: https://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_hide

Related

alert() not appearing on load in jQuery document ready handler

This is the code I use to show a message on my webpage.
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript" src="jquery-migrate-1.0.0.js"></script>
<script>
$(document).ready(function(e) {
alert('success');
});
</script>
However, after the page loads it shows me nothing.
try loading the jquery files from a cdn, unless you're already hosting it-
e.g.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
edit:
you say it's already apart of your server, I personally prefer making absolute paths to files..
e.g.
<script src="http://your.server.com/path/to/the/files/jquery.1.9.js"
obviously you'll need to get the actual path, and make sure it's got the correct permissions

showing jQuery is not defined even though jQuery file loaded

I am working on angular.js application in that I have loaded jQuery file still it shows error message
Error
Uncaught ReferenceError: jQuery is not defined(anonymous function) # popup.js:1
Files loaded
<script type="text/javascript"src="js/jquery-1.11.3.js"></script>
<script type="text/javascript" src="js/angular.js"></script>
<script type="text/javascript" src="js/angular-ui-router.js"></script>
<script type="text/javascript" src="js/angular-resource.js"></script>
<script src="js/app.js"></script>
<script src="js/appCtrl.js"></script>
I am not getting when is this popup.js is from.
Can anyone suggest me how to get rid of this error?
Move the jquery js reference to head tag or above the popup.js reference.
If you are working in laravel then add your CSS and Javascript file in index instead header and footer

JavaScript conflict preventing page from displaying correctly

I'm experiencing a conflict in my page with jquery scripts, the features(top menu or image slider) in the page does not open unless the page is reloaded several times.
http://www.in2info.com/leroyalcorporatev2/beirut/restaurants.php?v=4
Can someone help me fix it.
Thank you
Problem:
Uncaught ReferenceError: $ is not defined(anonymous function)
Uncaught TypeError: $ is not a function(anonymous function)
Cause:
Which could mean that wherever you're loading $ from (I'm assuming jQuery), might NOT be loading in time, thus when the site gets hit initially, jQuery isn't ready, and the menu will be broken.
Make sure you're adding jQuery in correctly
Example:
<head>
<script src="jquery-1.11.2.min.js"></script>
</head>
Look at your browser console debug window. I used chrome (press F12)
Jquery is not loaded and hence the issue.
Put your jquery reference under header as below:-
<script src="js/jquery-1.3.2.min.js"></script>
you use
<head>
.....
<script>
$(window).load(function(){
$.getScript("js/jquery-1.3.2.min.js");
});</script>
....
</head>
in your head tag to load jquery
$ is jquery
just put jquery straight into the head
e.g.
<head>
...
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
... //use $ now
</head>
after the script line jquery becomes available ( $ ).

document.getElementById('foo').src='' is not working

I have a website using twitter bootstrap. When developing sometimes the internet is not working so jquery won't be loaded and my site will not work. This is why I have a local copy of jquery in my js folder. I edited bootstrap files so if jquery is not detected the browser will change the location of jquery from cdn to my local file. I tried to do that but it is not working:Here is my code:
if (!window.jQuery) {
document.getElementById('jq').src='../../js/jquery.js';
console.log("You are using a backup file of jquery provided by our website");
}
else{
console.log("jQuery has been succesfully loaded by kounelios13");
}
html:
<script src="http://code.jquery.com/jquery-1.11.0.min.js" id="jq"></script>
<script src="../../js/bootstrap.js" type="text/javascript"></script>
<script src="../../js/prefixfree.js" type="text/javascript"></script>
<script src="../../js/apps/rgba.js" type="text/javascript"></script>
This is placed inside bootstrap's js file.
Any ideas???
Imaging that CDN file is not available. Then browser starts loading next script file (bootstrap.js), parser enters bootstrap.js, where you placed your check for jQuery. Your code detects that jQuery is not loaded and the script then changes src of the jq tag. It works fine and jQuery starts loading, however browser doesn't wait until jQuery is downloaded and carry on to the Bootstrap stuff, which fails immediately, because it requires jQuery. Hence the error.
You can try this version for backup jQuery library instead which should be reliable:
<script src="http://code.jquery.com/jquery-1.11.0.min.js" id="jq"></script>
<script>
if (!window.jQuery) {
document.write("<script src='../../js/jquery.js'>\x3C/script>");
}
</script>
<script src="../../js/bootstrap.js" type="text/javascript"></script>
<script src="../../js/prefixfree.js" type="text/javascript"></script>
<script src="../../js/apps/rgba.js" type="text/javascript"></script>
With jquery I don't thinks bootstrap script work so you need that.
If you have internet problem then you can copy jquery link and make file of that and attached.
ID must unique for the Document, you have another Element that uses the id jq or none.

Mimicking Browser's Script Loading and Executing Behaviour in Javascript

If I have,
<script src="jquery.js" ></script>
Then any script after this will allows you to use jQuery($) variable. I need to mimic this behavior in javascript. What I need that add my script file,
<script src="myscript.js" ></script>
which will load jquery.js using javascript and any script after this tag will allow you to use jQuery($) variable. I have no control beside myscript.js file. So I need to make sure that jquery.js must be loaded before allowing the browser to run/execute/render next tags.
What you are trying to achieve is not possible. When loading scripts dynamically you need to use callbacks to ensure that those scripts are loaded and use them only inside those callbacks. You cannot have sequential <script> tags in which one of the scripts loads dynamically some other scripts such as jquery and have subsequent scripts use jQuery. Browsers load script tags sequentially and guarantee that they will be loaded in the same order, but once you start injecting dynamic scripts into the DOM the only way to ensure proper load is to use callbacks. Take a look at the following article for more details.
Splitting it up in two parts should work:
index.html:
<html>
<body>
<script src="myscript.js"></script>
</body>
</html>
myscript.js:
document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>');
document.write('<script src="myprog.js"></script>');
myprog.js:
$(function(){
alert('jquery ready');
});

Categories

Resources