Arash Barez
Arash Barez

Reputation: 29

Where can I find the WordPress index.php file?

SO, It has been a few hours since I enqueued one custom script and one style sheet to my WordPress website functions. Enqueuing, those two worked perfectly; however, Chrome and Mozilla developer tools threw syntax error for my script. Below is the script.

<script>
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
  coll[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.display === "block") {
      content.style.display = "none";
    } else {
      content.style.display = "block";
    }
  });
}
</script>

Here is how I enqueued the script to my WordPress website functions.

function enqueue_my_custom_script() {
wp_enqueue_script( 'my-custom-js', 'path-to-the-file', false );
}

add_action( 'wp_enqueue_scripts', 'enqueue_my_custom_script' );

And lastly, here is the syntax error that Google and Mozilla Developer tools throw.

(index):160 Uncaught SyntaxError: Unexpected token '<'

Now, I have a series of questions.

  1. Why the script is not removed when I removed the queueing code from WordPress functions? I still see the script on page source code even after removing the function. (I see the JS file is not embedded in my website, but I see the actual script on my homepage. )
  2. Is it wrong to include <script></script> tags in a JS file for wordpress?
  3. The Google developer and Mozilla both indicates the code is on index.php, but I couldn't find it anywhere on my theme as it has multiple homepages templates. Where could this file be located?
  4. the fourth one and the most important of all, how can I get rid of this error and the script?

Upvotes: 2

Views: 323

Answers (2)

mister_cool_beans
mister_cool_beans

Reputation: 1533

You don't include <script></script> tags in JavaScript files. <script> is a HTML tag, your JavaScript file is not a HTML file.

Upvotes: 0

Abbas Akhundov
Abbas Akhundov

Reputation: 562

  1. It is probably cache. Clear it and you should be fine.
  2. Not only for WordPress, but in general within .js file. One would use <script></script> tags to either include .js file or write js code between the tags. In the last option, you specify that this is javascript code, and should be handled as one.
  3. index.php is simply a gateway into your Wordpress site. So all errors will show it being related to index.php in the error trace.
  4. Remove script tags from your .js file

Upvotes: 1

Related Questions