Namespaces
Variants
Actions
Revision as of 07:39, 1 August 2012 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Canceling the default action in Browser 7.1

Jump to: navigation, search


Article Metadata

Tested with
Devices(s): All devices using Browser 7.1

Compatibility
Platform(s): S60 3rd Edition FP2, S60 5th Edition

Article
Keywords: onclick, event.preventDefault
Created: User:Kbwiki (02 Nov 2009)
Last edited: hamishwillee (01 Aug 2012)


Contents

Overview

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) {
event.preventDefault();
//event.stopPropogation();
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]]
130 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved