Problem: launching javascript from hyperlinks without redirecting
2 min read
2 min read
<a id="myLaunchLink" href="#" onclick="myJavaScriptFunction();">click me</a>In such a case everything would be fine unless you reuse that piece, i.e. it is placed inside a user control which may appear multiple times on a page. In that case you'll get a problem with the id of the link since you have the situation where more links with the same id "myLaunchLink" reside on the same page.
a { ... }don't apply. So the href element has to stay.
<asp:hyperlink href="javascript:;" id="myLaunchLink" runat="server" onclick="myJavaScriptFunction();">Suche</asp:hyperlink>One may now argue why not to launch the "myJavaScriptFunction" call directly in the href part instead of just doing nothing there. Well that's true in this simple case but it may change when you attach the javascript event-handler dynamically from within another javascript function or ajax extender.