6

For the sake of debugging the javascript-part of a Rails 6 (version 6.0.0.rc1) web application I want to use my custom javascript functions also in the Chrome console (aka. Inspect).

Back when I used just static HTML files to build a website (as opposed to using a web-framework like Rails as of right now) you would simply embed the JS file in the DOM like this

<!-- custom JS -->
<script src="js/custom.js"></script>

and could instantly access and execute all custom functions that were placed in this file.

Background: The JS file is placed at the correct rails 6 specific directory as provided in this article: How to require custom JS files in Rails 6

Note: The rails 6 application also uses the JS file already, since the browser shows the console log message.

Here is the full content of the JS file:

// app/javascript/packs/custom.js
console.log("loaded custom.js successfully")

function sayHello () {
  console.log("Hello good Sir or Madam!")
}

Expectation: I am expecting to open the browser's (Chrome) console and be able to use the sayHello() function in the console.

However, when I do so, I get an error message in the console stating:

Uncaught ReferenceError: sayHello is not defined
TheCodeBat
  • 61
  • 2

1 Answers1

0

Try something like

sayHello = ()=>{
console.log("Hello good Sir or Madam!");
}

then you can evoke in console:

>sayHello();
  • Welcome to Stack Overflow! Thank you for this code snippet, which might provide some limited short-term help. A proper explanation of why this works and the question code doesn't [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Mar 06 '21 at 14:43