1

If I use script tags with my code in it at the bottom beneath the closing body tag the javascript executes like it should. However I want to write many functions for other things to happen on the page and so it gets busy if I do it all on script tags, which is why I want separate external files for where the code lives and then in the head tags invoke my code with script src, etc.

I am running rails 5.2.2 and it isn't loading my external js files at all. At first I thought maybe it was broken code but alas I can't even get a simple alert event to work so I know it's not the code and in addition my function works when I stick it in script tags on the html view. Help! I am sort of a nascent hobbyist programmer and not the best of googlers so any and all feedback is appreciated.

<!DOCTYPE html>
<head>
  <script src="edit_cp.js"></script>
</head>
<body>
...
</body>
</html>

application.js file

//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require_tree .

edit_cp.js file

var editCoverPhoto = document.getElementById("cp_button");
editCoverPhoto.addEventListener("mouseover", showMessage);
editCoverPhoto.addEventListener("mouseleave", removeMessage);
var para = document.createElement("p");
var textNode = document.createTextNode("Edit Cover Photo");
para.appendChild(textNode);

function showMessage() {
  editCoverPhoto.appendChild(para);
  para.style.color = "black";
}
function removeMessage() {
  editCoverPhoto.removeChild(para);
}
10
  • I think this might be helpful for your case. stackoverflow.com/questions/12531783/… Commented Jun 13, 2019 at 22:03
  • 1
    I didn't quite find a solution in that question thread but thanks for sharing. I am stymied becuase in earlier versions of rails and previous projects I've had no problem with this. I'm just trying to use a little vanilla js for page events like mouseover and mouseleave when you hover over an icon, a new dom element is created displaying a message of what action you'll accomplish by clicking on the icon, etc. Commented Jun 14, 2019 at 0:58
  • 1
    Alright, I'll give that a go tomorrow. I feel like that's redundant telling rails to look for the path when it already should do that lol but you might be onto something. If it works, I'll let you know and you can turn your comment into an answer which I'll in turn mark the question as answered Commented Jun 14, 2019 at 5:40
  • 1
    Thank you thank you thank you! At first it didn't work when I tried to load the script in the head tags but then I just moved it to after the body close tag and my javascript ran like it should. Commented Jun 14, 2019 at 17:34
  • 1
    Thanks again for pointing me in the right direction. I was seriously beginning to question everyhing lol ;) coding is fun.... Commented Jun 15, 2019 at 4:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.