Jump to content

Uncaught ReferenceError: $ is not defined, Invision 4.6.12.1


Go to solution Solved by Martin A.,

Recommended Posts

Hi im currently having difficulty getting a jquery object to function and the console keeps throwing Uncaught ReferenceError: $ is not defined when doing this inside a custom PHP block

var table = $("#anscestryMainContainer").tableSortable(options);

how do i go about fixing this

Link to comment
Share on other sites

seems a workaround is to upload custom jquery/js files and directly reference them 1 line above as the jquery file referenced by ips is strangely considered out of scope for some reason and not allowing the $ variable to be accessed post ips access

Link to comment
Share on other sites

  • Solution

Wait for DOM to be loaded before doing anything in your JS. jQuery is loaded at the end of the document, which is why it isn't available at the time your PHP block is executed.

Loading a second set of jQ libraries is not the solution here.

Edited by Martin A.
Link to comment
Share on other sites

That was the norm a couple of years ago, but it's not the practice anymore for speed and SEO reasons.

https://levelup.gitconnected.com/all-about-script-87fea475b976

Quote

The best practice is to put JavaScript <script> tags just before the closing </body> tag rather than in the <head> section of your HTML.

The reason for this is that HTML loads from top to bottom. The head loads first, then the body, and then everything inside the body. If we put our JavaScript in the head section, the entire JavaScript will load before loading any of the HTML, which could cause a few problems :

  1. If you have code in your JavaScript that alters HTML as soon as the JavaScript code loads, there won’t actually be any HTML elements available for it to affect yet, so it will seem as though the JavaScript code isn’t working, and you may get errors.
  2. If you have a lot of JavaScript, it can visibly slow the loading of your page because it loads all of the JavaScript before it loads any of the HTML.

When you place your JavaScript at the bottom of your HTML body, it gives the HTML time to load before any of the JavaScript loads, which can prevent errors, and speed up website response time.

 

Edited by Martin A.
Link to comment
Share on other sites

I can partially see your point for run of the mill js but with a commonly used library like jQuery <head> is more relevant otherwise you have to throw in many window.onload() function wrappers usually bound to anonymous functions just to "wait". In small cases sure this is not a problem but when a user accesses the $ variable many times that's many more randomly inserted wrappers needed to "wait" and increases both file size and potentially load time, not to mention if someone else gets brought in on the project and sees all these wait wrappers strewn about the place they will have to have more effort to bring them up to speed.

Link to comment
Share on other sites

This is not an issue with JS files that follow the coding standards of the software. The framework will halt the execution for us. I rarely, or rather never(?), need to wrap anything in an onload event.

You might be better off creating a plugin or an app if you plan on adding lots of javascript to the PHP block.

 

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...