How to make JavaScript work on a specific page

Operating environment Ruby 2.6.5 Rails 6.0.3.2

Basically, JavaScript occurs on all pages, so even on pages that do not use JavaScript, JavaScript errors will occur in the console of the verification tool. I finally solved it, so I posted it.

Code required for implementation

hoge.js


if (location.pathname.match("hoge")){
  //Start writing JavaScript from here.
}

Now hoge.js will only work with the path hoge.

For the time being, if you put a commentary, you can get the current path by location.pathname and check if it matches hoge by match.

Personally, I was worried about JavaScript errors in the console, so this was neat.

Recommended Posts

How to make JavaScript work on a specific page
[Rails] How to load JavaScript in a specific view
How to make a Java container
How to make a JDBC driver
How to make a splash screen
How to make a Jenkins plugin
How to make a Maven project
How to make a Java array
How to make a Java calendar Summary
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
How to make a Discord bot (Java)
How to deploy a container on AWS Lambda
How to make rbenv recognize OpenSSL on WSL
How to display a web page in Java
How to make a lightweight JRE for distribution
How to make a follow function in Rails
How to build a Pytorch environment on Ubuntu
How to make shaded-jar
How to make a factory with a model with polymorphic association
How to make an almost static page with rails
[Java] How to execute tasks on a regular basis
[Java] How to erase a specific character from a character string
How to make a cache without thinking too much
How to make a mod for Slay the Spire
How to create a header or footer once and use it on another page
Java --How to make JTable
How to deploy on heroku
How to leave a comment
[Rails] How to make seed
How to insert a video
How to create a method
How to deploy a simple Java Servlet app on Heroku
How to get inside a container running on AWS Fargate
I want to make a specific model of ActiveRecord ReadOnly
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to deploy a kotlin (java) app on AWS fargate
How to display a graph in Ruby on Rails (LazyHighChart)
How to install GNOME as a desktop environment on CentOS 7
Apply CSS to a specific View in Ruby on Rails
How to deploy a Rails application on AWS (article summary)
How to deploy jQuery on Rails
How to add columns to a table
How to deploy Laravel on CentOS 7
Try to make a simple callback
How to "hollow" View on Android
How to make a jar file with no dependencies in Maven
[Java] How to update Java on Windows
How to use Ruby on Rails
How to sign a Minecraft MOD
How to deploy Bootstrap on Rails
How to run JavaFX on Docker
How to automatically operate a screen created in Java on Windows
How to use Bio-Formats on Ubuntu 20.04
[Java] How to create a folder
How to write a ternary operator
How to install MariaDB 10.4 on CentOS 8
Rails on Tiles (how to write)
[Swift] How to send a notification
How to install WildFly on Ubuntu 18.04
[Rails] How to put a crown mark on the ranking function