Jump to content

Help with Jquery not being Defined

Guest

Jquery is Loaded (in <script> tag).

 

here is the Script loader 

var script = document.createElement('script');
script.src = '/src/jquery-embed.js';
script.type = 'text/javascript';
var head = document.getElementsByTagName("head")[0]
head.insertBefore(script, head.firstChild);
// use any jquery code here
// error normally "$ is not defined" aka jquery not found.

How do i define jquery to able to use jquery inside of the JS file?

My code

(before execute)

<head>
<script src="/cool/js.js"></script>
</head>
<body>
<script>$(body).css(*******)</script>
</body>

(after execute)

<head>
<script src="/cool/jquery.js">
<script src="/cool/js.js"></script>
</head>
<body>
<script>$(body).css(*******)</script>
<!-- no error -->
</body>

 

Link to comment
Share on other sites

Link to post
Share on other sites

Why do you load js from another js? You can just include jQuery from a CDN with a script tag (https://jquery.com/download/#using-jquery-with-a-cdn, example code with Google CDN is here https://developers.google.com/speed/libraries#jquery) or if you don't want to use a CDN, then you can use the local/relative path instead of a full URL.

HAL9000: AMD Ryzen 9 3900x | Noctua NH-D15 chromax.black | 32 GB Corsair Vengeance LPX DDR4 3200 MHz | Asus X570 Prime Pro | ASUS TUF 3080 Ti | 1 TB Samsung 970 Evo Plus + 1 TB Crucial MX500 + 6 TB WD RED | Corsair HX1000 | be quiet Pure Base 500DX | LG 34UM95 34" 3440x1440

Hydrogen server: Intel i3-10100 | Cryorig M9i | 64 GB Crucial Ballistix 3200MHz DDR4 | Gigabyte B560M-DS3H | 33 TB of storage | Fractal Design Define R5 | unRAID 6.9.2

Carbon server: Fujitsu PRIMERGY RX100 S7p | Xeon E3-1230 v2 | 16 GB DDR3 ECC | 60 GB Corsair SSD & 250 GB Samsung 850 Pro | Intel i340-T4 | ESXi 6.5.1

Big Mac cluster: 2x Raspberry Pi 2 Model B | 1x Raspberry Pi 3 Model B | 2x Raspberry Pi 3 Model B+

Link to comment
Share on other sites

Link to post
Share on other sites

Its been a while since i did anything like this but i think jquery is only accessible from your /cool/js.js

I think it is inserting a reference for the jquery source into <head>, while the <head> code is running, i.e. after head code has already been loaded into the program. So the jquery reference never gets loaded/registered.

Link to comment
Share on other sites

Link to post
Share on other sites

sorry, what i ment was Auto-append Jquery, When i append it from /my/js.js i get the "$ is not defined" error

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Mling said:

Its been a while since i did anything like this but i think jquery is only accessible from your /cool/js.js

I think it is inserting a reference for the jquery source into <head>, while the <head> code is running, i.e. after head code has already been loaded into the program. So the jquery reference never gets loaded/registered.

how ould i go about doing this?

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Nolanrulesroblox said:

how ould i go about doing this?

you need to put  <script src="/cool/jquery.js"> into your html file. Inserting it into the DOM after <head> is loaded is not how web pages work.

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Mling said:

you need to put  <script src="/cool/jquery.js"> into your html file. Inserting it into the DOM after <head> is loaded is not how web pages work.

no no no, Ok So Jquery is already appended to the Head in my Script, but jquery is not loaded

// /cool/js.js
var script = document.createElement('script');
script.src = '/src/jquery-embed.js';
script.type = 'text/javascript';
var head = document.getElementsByTagName("head")[0]
head.insertBefore(script, head.firstChild);

$("body").append("text");
// $ is not defined

 

Link to comment
Share on other sites

Link to post
Share on other sites

I don't know any simpler way of explaining this. Your head section is running a script that inserts code into the head section. The head section of the DOM, and the reference section of any code, is not a living object. The head section gets run once. You cannot insert things into the head section after the browser has loaded it. You need to code the reference to jquery.js in the html file., not the js file.

Link to comment
Share on other sites

Link to post
Share on other sites

31 minutes ago, Mling said:

I don't know any simpler way of explaining this. Your head section is running a script that inserts code into the head section. The head section of the DOM, and the reference section of any code, is not a living object. The head section gets run once. You cannot insert things into the head section after the browser has loaded it. You need to code the reference to jquery.js in the html file., not the js file.

All i got to say is poo. Huge thanks to all of you for trying to help. (it was a long shot)

Link to comment
Share on other sites

Link to post
Share on other sites

You can just add this in your <head>.

Alternatively, you can download the .js file.

<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

 

Link to comment
Share on other sites

Link to post
Share on other sites

20 hours ago, darkDOS said:

You can just add this in your <head>.

Alternatively, you can download the .js file.


<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

 

i know i can do that, but i wanted to have my script Insert Jquery automatically. like $.getScript (and use jquery within the same file)

Link to comment
Share on other sites

Link to post
Share on other sites

18 hours ago, Nolanrulesroblox said:

i know i can do that, but i wanted to have my script Insert Jquery automatically. like $.getScript (and use jquery within the same file)

i am going to go with requirejs, it does what i need it to do, but Having the same issue, $ is not defined

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×