Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Canceling the default action in Browser 7.1

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.

Article Metadata
Tested with
Devices(s): All devices using Browser 7.1
Platform(s): S60 3rd Edition FP2, S60 5th Edition
Keywords: onclick, event.preventDefault
Created: User:Kbwiki (02 Nov 2009)
Last edited: lpvalente (26 Jul 2014)



On devices using Browser 7.1, using {{{1}}} without preventing the default action causes the page to scroll back to the start. This will cause navigational problems especially in widgets using the tabbed navigation mode.

This article demonstrates how the default action can be canceled, depending on how events are handled. Canceling the default action prevents the browser searching for the # anchor, so that the page view position is not changed.

Inline event handler

The inline event handler is the oldest way to attach events to elements.

<a id="inline" href="#" onclick="inlineEvent();">scroll</a>

However, the empty # anchor causes the browser to scroll the page back to top. For inline event handlers, the default action can be canceled by simply returning a false onclick event.

<a id="inline" href="#" onclick="inlineEvent(); return false;">No scroll</a>

Traditional event handler

In a traditional event handler, the most of the work is done on the JavaScript side. The default action can be canceled by simply returning False from the event handler.

var elFalse = document.getElementById("elFalse");
elFalse.onclick = function(){
alert("page not scrolled. False returned");
return false;
<a id="elFalse" href="#">click me</a>

DOM level 2 event handler

The DOM level 2 event handler allows multiple event handlers to attach to the same event. In addition, it provides a more sophisticated control for canceling the default action.

var elDOMII =document.getElementById("elDOMII");
elDOMII.addEventListener( "click", iwasclicked, false );
function iwasclicked(event) {
alert("page not scrolled default action prevented");
<a id="elDOMII"  href="#">Event Link dom 2 </a>

JavaScript pseudo-URL

It is also possible to use the JavaScript pseudo-URL as the href target. However, the javascript: protocol is not a public standard. It functions similarly to writing a JavaScript directly in the browser's address bar: javascript:alert("Try me!");

Remember that javascript pseudo-URL should only be used in places where you would normally enter an URL.

function pseudourl()	{
alert("called from pseudourl");
<a href="javascript:pseudourl();">click me</a>
<code>[[Category:JavaScript]][[Category:S60 3rd Edition FP2]][[Category:S60 5th Edition]]
This page was last modified on 26 July 2014, at 23:26.
26 page views in the last 30 days.