Skip to main content
Participant
October 21, 2019
Answered

SOLVED: Javascript "onmouseover" does not work

  • October 21, 2019
  • 1 reply
  • 630 views

This is a follow up on a problem that was encountered yesterday. The original post can be found here:

https://community.adobe.com/t5/Dreamweaver/Javascript-onmouseover-does-not-work/td-p/10681988

 

In a few words the problem was that a code I had written in Javascript for my website did not work in Dreamweaver HTML code, whereas it worked in jsfiddle and in theory also in W3schools site.

The problem was solved when I moved the Javascript code to the bottom of the HTML code, right before the </body> tag. Previously I had linked it through <script src"..."></script> at the head and it didn't work. As soon as I moved it as the last line inside the <body> it functioned as it should.

 

I am new in coding and I haven't quite worked with scripts at both the <head> and at the <body> as it is mentioned here https://www.w3schools.com/js/js_whereto.asp , and why one works and the other doesn't, but general consensus as far as I a have read is for the script to be put at the bottom of the page.

 

If anyone has any insight on the matter, please share it so that people like me with zero experience in coding can benefit.

 

Cheers,

Daniel

    This topic has been closed for replies.
    Correct answer osgood_

    If you insert the javascript at the top of the page it runs before the whole page has finished loading therefore it has no effect on the elements you wish to add events to. If you insert it at the bottom it runs after the page had loaded and therefore it has an effect on the elements you wish to add events to.

     

    1. You can add the defer attribute to the script tag if you wish to keep the javascript at the top of the page.  This will have the effect of only evoking the javascript after the rest of the page has loaded.

     

    <script src="blah.js" defer></script>

    1 reply

    osgood_Correct answer
    Legend
    October 21, 2019

    If you insert the javascript at the top of the page it runs before the whole page has finished loading therefore it has no effect on the elements you wish to add events to. If you insert it at the bottom it runs after the page had loaded and therefore it has an effect on the elements you wish to add events to.

     

    1. You can add the defer attribute to the script tag if you wish to keep the javascript at the top of the page.  This will have the effect of only evoking the javascript after the rest of the page has loaded.

     

    <script src="blah.js" defer></script>

    Participant
    October 21, 2019

    Tried it and works like a charm! Thanks!