I am using this code to only run this js on certain pages.
$("body#action_new").ready(function() {
console.log($("body#action_new"));
console.log($("body#action_new").length);
console.log("code is running");
}
Even though body#action_new does not exist, the code is running.
the two console.logs print out:
[]
0
code is running
What gives?
You cannot call the
readyexcept on the document, intead you can tryAs stated by @Interrobang in the comments the
.size()method internally uses.lengthso it is advised to use.lengthto avoid the additional overhead of a function call, so the above code would look like