开发者

Cancel Browser Scroll Reset for Inline Onclick

开发者 https://www.devze.com 2022-12-27 16:32 出处:网络
I\'m using prototype to construct an ajax request from an inline onclick on a link.The function shows an element on the page and this all works fine, except that the browser scroll bar resets to the t

I'm using prototype to construct an ajax request from an inline onclick on a link. The function shows an element on the page and this all works fine, except that the browser scroll bar resets to the top after every click of the link. This is bad b/c sometimes the content that I want to show is partially scrolled down the page and this makes it so the use开发者_JAVA技巧r cannot see it. I realize it's better to not include the onclick inline, but I have a situation where I dynamically create these links and then cache the html so it is faster this way. Is there any way to cancel this browser scroll reset using an inline onclick? 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 { }
}


Your onclick needs to return false to stop the browser from reloading the page (resulting in scrolling to the top). Try:

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号