Cancel Reset Scrolling for Inline Onclick

I am using a prototype to create an ajax request from the onclick inline link. The function shows an element on the page, and all this works fine, except that the browser scrollbar is reset to the top after each click of the link. This is bad b / c, sometimes the content that I want to show partially scrolls around the page, and this makes it so that the user cannot see it. I understand that itโ€™s better not to enable the onclick built-in, but I have a situation where I dynamically create these links and then cache the html, so this is faster. Is there a way to cancel this browser scroll reset using the onclick built-in? This is what my code looks like:

<a href="#" rel="nofollow" onclick="linkAjax('example')" id="word_link_example">example</a> function linkAjax(word){ if(!making_ajax_request){ making_ajax_request = true; new Ajax.Request('/example/test', {parameters:{data: word}, onLoading: function(){searchLoading();}, onComplete: function(){ making_ajax_request = false; } }); } else { } } 
+1
javascript prototypejs scrollbar
source share
2 answers

Your onclick should return false so that the browser does not reload the page (as a result, it scrolls up). Try:

 <a href="#" rel="nofollow" onclick="linkAjax('example'); return false;" id="word_link_example">example</a> 
+1
source share

Do this: onclick = "linkAjax ('example') return false"

0
source share

All Articles