最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

internet explorer - javascript void(0) problem in IE - Stack Overflow

programmeradmin0浏览0评论

HI,

I am developing a web page using asp.

I am using some links in my web page. For that I have used some code like this.

<a href="javascript:void(0);" onclick="javascript:ChangeLoc('TEST','');">Test</a>

and in the ChangeLoc() method I have written __doPostBack event.

This works fine in IE7 installed in my machine. But in IE6 in another machine it does not invoke the __doPostBack event.

Edit

When I change the void(0) in href it works fine.

I would like to know whether it is a bug with IE or a JavaScript problem.

function ChangeLoc( param, arg )
{
     __doPostBack ( param, arg )
}

HI,

I am developing a web page using asp.net.

I am using some links in my web page. For that I have used some code like this.

<a href="javascript:void(0);" onclick="javascript:ChangeLoc('TEST','');">Test</a>

and in the ChangeLoc() method I have written __doPostBack event.

This works fine in IE7 installed in my machine. But in IE6 in another machine it does not invoke the __doPostBack event.

Edit

When I change the void(0) in href it works fine.

I would like to know whether it is a bug with IE or a JavaScript problem.

function ChangeLoc( param, arg )
{
     __doPostBack ( param, arg )
}
Share Improve this question edited Feb 1, 2010 at 9:07 rahul asked May 8, 2009 at 4:45 rahulrahul 187k50 gold badges238 silver badges264 bronze badges 3
  • What does your ChangeLoc() function look like? – pkaeding Commented May 8, 2009 at 4:51
  • This makes me think... why is there three camps of people who work with <a href> and javascript? One likes to have javascript:[code here] in the href, another likes to have void(0) and have all the code in onclick, and then there's #... – unrelativity Commented May 8, 2009 at 7:39
  • 1 There are four camps. The three you describe, and the group who are correct and use progressive enhancement: icant.co.uk/articles/pragmatic-progressive-enhancement – Quentin Commented May 8, 2009 at 8:53
Add a comment  | 

4 Answers 4

Reset to default 12

href and onclick both get fired when you click an element, you are overwriting the onclick event with void()

change to

<a href="#" onclick="ChangeLoc();return false">test</a>

or with jQuery.

$(function(){
  $("#linkId").click(function(event){
      ChangeLoc();
      event.preventDefault();
  });
}); 

Do you get an error? If so, what error do you get in IE6? Can you post the code for ChangeLoc()? Also, try changing your markup to the following and see if you get the same result:

<a href="#" onclick="ChangeLoc(); return false;">Test</a>

Edit: removed 'javascript:' from the onclick

You can also use unobtrusive javascript syntax:

<a href="index.html" id="chngLink">test</a>

<script type="text/javascript">
document.getElementById("chngLink").onclick = function(e) {
    if (e && e.preventDefault) {
        e.preventDefault();
    }
    ChangeLoc('TEST','');
    return false;
};
</script>

it is not good to use <a>-element for javascript functions call. Use styled <span onclick="my_function()" class="looks_like_hyperlink">...</span>

发布评论

评论列表(0)

  1. 暂无评论