Javascript not triggering

Hi all, I have a simple piece javascript code to keep the scroll bar of a container in the down position when you view it (because default is in the up position): var x = document.getElementsByClassName("dataViewConversation")[0];  x.scrollTop = x.scrollHeight;   Problem is that when I use either the HTML snippet or JavaScript Snippet available in the app store the js does not trigger on view, but I know it works because I tried it with the javascript button to manually trigger and it works fine. Any ideas?
2 answers

Probably a loading issue. The HTML snippet is loaded before the part you need, hence the getElementsByClassName("dataViewConversation")[0]; does not find an element. You can of course check this by adding a console.dir(getElementsByClassName("dataViewConversation")[0]); in your JS code, to see if the code actually triggers. It probably is, but too early, hence giving an empty in the console.dir message.

So, best is to add a timeout to the JS script, to make sure the JS script is triggered AFTER the page is loaded. 


Hey Jon,

Are you sure that the JavaScript is not only loaded, but also triggered when the page is loaded? Usually you need to trigger the JavaScript somehow for it to be executed. To trigger it onload of the page, you could use something like:

window.onload = function() {
  yourFunction(param1, param2);

I sounds like your execute JavaScript button makes it trigger just fine, but otherwise it has no reason to be executed.

PS: getElementsByClassName returns a HTMLCollection of elements, so it could be possible that your JavaScript won't allways work. If you want a specific element on a page, use getElementById instead.

Kinds regards,