Roberto89 Roberto89 - 3 months ago 10
Javascript Question

How to disable js script on specific page?

Idea: I want to disable js script on specific page(e.g

new_post_path
), that is placed in
application.js
. On another page script should works. Have you any idea how to solve this problem? I thought to use loop
unless current_page?(new_post_path)
, but it's not work in js file.

Simple script to disable:

window.setTimeout(function() {
$(".alert").fadeTo(500, 0).slideUp(500, function() {
$(this).remove();
});
}, 10000);
end

Geo Geo
Answer

You could set two attributes in the layout:

<body data-controller="<%= controller_name %>" data-action="<%= action_name %>">

</body>

and from your js, check the value of data-controller and data-action to know whether or not you want to run the script on that action.

Comments