473,385 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Trying to run a function from onclick without activating the link

I have the following link on a web page

<p class="menuitem"><a href="#"
onclick="loadFragment('http://rivendellweb.net/fortress/home',
'index')" title="The Fortress Home">The Fortress Home</a></p>

Dec 25 '06 #1
10 4037
VK
Carlos Araya wrote:
I have the following link on a web page

<p class="menuitem"><a href="#"
onclick="loadFragment('http://rivendellweb.net/fortress/home',
'index')" title="The Fortress Home">The Fortress Home</a></p>
<p class="menuitem"
onclick="loadFragment('http://rivendellweb.net/fortress/home',
'index')"
title="The Fortress Home">The Fortress Home</p>

P.S. It is convenient for respondents to see the actual
question/problem in the message body as well, not in the title only.

Dec 25 '06 #2
Carlos Araya said the following on 12/25/2006 5:47 PM:
I have the following link on a web page

<p class="menuitem"><a href="#"
onclick="loadFragment('http://rivendellweb.net/fortress/home',
'index')" title="The Fortress Home">The Fortress Home</a></p>
<a href="http://rivendellweb.net/fortress/home"
title="The Fortress Home"
onclick="loadFragment(this.href,'index');alert('ig nore VK');return false">
The Fortress Home</a>

The trick to "cancel navigation" is to return false in the event handler.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 25 '06 #3
VK
Randy Webb wrote:
.... alert('ignore VK') ...

<http://jsnet.sourceforge.net/tmp/irw.html>

Dec 26 '06 #4
Randy:

I tried tagging the ;return false statement at the end of my onclick
handler and it still activates the link. I'm using Firefox 2.0 and
Safari 1.3 to test the page if that makes a difference.
Randy Webb wrote:
Carlos Araya said the following on 12/25/2006 5:47 PM:
I have the following link on a web page

<p class="menuitem"><a href="#"
onclick="loadFragment('http://rivendellweb.net/fortress/home',
'index')" title="The Fortress Home">The Fortress Home</a></p>

<a href="http://rivendellweb.net/fortress/home"
title="The Fortress Home"
onclick="loadFragment(this.href,'index');alert('ig nore VK');return false">
The Fortress Home</a>

The trick to "cancel navigation" is to return false in the event handler.
Dec 26 '06 #5
ASM
Carlos a écrit :
Randy:

I tried tagging the ;return false statement at the end of my onclick
handler and it still activates the link. I'm using Firefox 2.0 and
Safari 1.3 to test the page if that makes a difference.
don't try with refresh button of your navigator
and before to reload verify the url in address bar doesn't finish with '#'

usually return false; aborts link (FF, Safari, IE ... )
Randy Webb wrote:tress Home">The Fortress Home</a></p>
>>>
<a href="http://rivendellweb.net/fortress/home"
title="The Fortress Home"
onclick="loadFragment(this.href,'index');return false">
The Fortress Home</a>

The trick to "cancel navigation" is to return false in the event handler.


--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Dec 27 '06 #6
I tried it quiting the browser every time and even emptying the cache
on both browsers.

My question is then how can I prevent the link to be activated when
javascript is active? The result I want is to have the javascript
function activated with onclick to load it's content when Javascript is
enabled but the actual href= link to work when it isn't.

Do you have any suggestion?

Carlos
ASM wrote:
Carlos a écrit :
Randy:

I tried tagging the ;return false statement at the end of my onclick
handler and it still activates the link. I'm using Firefox 2.0 and
Safari 1.3 to test the page if that makes a difference.

don't try with refresh button of your navigator
and before to reload verify the url in address bar doesn't finish with '#'

usually return false; aborts link (FF, Safari, IE ... )
Randy Webb wrote:tress Home">The Fortress Home</a></p>
>>
<a href="http://rivendellweb.net/fortress/home"
title="The Fortress Home"
onclick="loadFragment(this.href,'index');return false">
The Fortress Home</a>

The trick to "cancel navigation" is to return false in the event handler.

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Dec 27 '06 #7
VK

Carlos wrote:
I tried it quiting the browser every time and even emptying the cache
on both browsers.

My question is then how can I prevent the link to be activated when
javascript is active? The result I want is to have the javascript
function activated with onclick to load it's content when Javascript is
enabled but the actual href= link to work when it isn't.
false has to be returned from the *event handler* for that, as it was
suggested. In case like
<a href="somePage.html" onclick="doSomething(); return false;">Click
me</a>
the (intrinsic) event handler is whatever you have in quotes after
onclick=.

doSomething function may return false, true, nothing or the purpose of
this world - the system couldn't care lesser, the link will be still
followed.

At the same time if you assign a handler pragrammatically like:
document.links[22].onclick = doSomething;
and doSomething returns false it *will* prevent the navigation.

No, UA makers were not on drugs while making it (at least not in this
case :-), only rather careless of usability.

In the first case you have inline handler right in the tag "onclick"
attribute. On parsing it becomes anonymous function, so say
onclick="doSomething();"
becomes
onclick = function() {
doSomething();
}
This way you can see that no matter what doSomething returns, the
anonymous function itself returns nothing (undefined) so the navigation
is not prevented.
onclick="doSomething(); return false;"
does what expected.

At the same time
linkObject.onclick = doSomething;
directly assign onclick to doSomething, so whatever return value of
doSomething - this is return value of the event handler.

Dec 27 '06 #8
VK said the following on 12/27/2006 4:14 PM:
Carlos wrote:
>I tried it quiting the browser every time and even emptying the cache
on both browsers.

My question is then how can I prevent the link to be activated when
javascript is active? The result I want is to have the javascript
function activated with onclick to load it's content when Javascript is
enabled but the actual href= link to work when it isn't.

false has to be returned from the *event handler* for that, as it was
suggested.
It does?

<a href="http://www.google.com" onclick="return theAction()">
Don't go to Google
</a>

function theAction(){
return false;
}

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 27 '06 #9
VK

Randy Webb wrote:
<a href="http://www.google.com" onclick="return theAction()">
Don't go to Google
</a>
function theAction(){
return false;
}
Right, that's the most common. I just wanted to be more "linear" with
the step-by-step explanations.

To OP: the same with form validation:

[WRONG]
function validate(frm) {
// do stuff
// if failed then
return false;
}
....
<form ... onsubmit="validate(this)">

[right]
<form ... onsubmit="return validate(this)">

[BUT]
formObject.onsubmit = validate;
// works fine

Dec 27 '06 #10
VK:

thanks, I added return false to load fragment and that fixed the
problem.

Carlos

VK wrote:
Carlos wrote:
I tried it quiting the browser every time and even emptying the cache
on both browsers.

My question is then how can I prevent the link to be activated when
javascript is active? The result I want is to have the javascript
function activated with onclick to load it's content when Javascript is
enabled but the actual href= link to work when it isn't.

false has to be returned from the *event handler* for that, as it was
suggested. In case like
<a href="somePage.html" onclick="doSomething(); return false;">Click
me</a>
the (intrinsic) event handler is whatever you have in quotes after
onclick=.

doSomething function may return false, true, nothing or the purpose of
this world - the system couldn't care lesser, the link will be still
followed.

At the same time if you assign a handler pragrammatically like:
document.links[22].onclick = doSomething;
and doSomething returns false it *will* prevent the navigation.

No, UA makers were not on drugs while making it (at least not in this
case :-), only rather careless of usability.

In the first case you have inline handler right in the tag "onclick"
attribute. On parsing it becomes anonymous function, so say
onclick="doSomething();"
becomes
onclick = function() {
doSomething();
}
This way you can see that no matter what doSomething returns, the
anonymous function itself returns nothing (undefined) so the navigation
is not prevented.
onclick="doSomething(); return false;"
does what expected.

At the same time
linkObject.onclick = doSomething;
directly assign onclick to doSomething, so whatever return value of
doSomething - this is return value of the event handler.
Dec 28 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dennis M. Marks | last post by:
I have seen partial answers to the following question but I am not sure of the best approach. I have a page that will only work with javascript. The link to the page indicates that. Within the...
10
by: Nom DePlume | last post by:
What I am trying to should be everyday simple, but I just can't seem to grasp it. What I want to do is click a link and have that link send a URL to a function to open a new widow of a certain...
3
by: Samir | last post by:
1. Here is my javascript <script language="JavaScript"> function link(linkname){ strvar1 = linkname window.main.location=""+strvar1+".asp" } </script> 2. Here is part of my code
2
by: Mark Preston | last post by:
Its perhaps a bit early to ask, since I'm still doing the page design and haven't got down to the coding yet, but I wonder if anyone can help with a bit of a question. To lay the groundwork, a...
3
by: 2obvious | last post by:
Below is a stripped down example of code I'm using to count links in a list. My intent with the ShowLinkNumber() function was to display the number of the current link. (e.g. click on link 2, and...
26
by: johkar | last post by:
I need to cancel the link and execute a function onclick of all the links within the span tag which has a class of "container" assigned. There will be only one span tag with this class applied. ...
10
by: ljlolel | last post by:
So.. I have a form that submits to an ASP.net site made in C-sharp. The ASP site is not mine, i do not have the server side code. When I submit from my form by pressing the Submit button, I get...
8
by: optimistx | last post by:
In excellent YAHOO user interface programs I see often extra parenthesis like this (foo=function(){ alert('I am foo'); })(); instead of bar=function(){
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.