473,471 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

onClick and href="#"

Jez
Hi,

I've created a function which opens a popup window containing a
calendar. When a day is clicked, the date is entered into a text box
on the parent page and the popup is closed.

The link I'm using on the parent page is ...

<a href="#" onClick="popupcal()">choose date</a>

My form is quite long, and when the popup is opened I'm returned to
the top of the page meaning I then have to scroll down again to
continue entering information.

I'm assuming this is due to the href="#" part of the link? Is there
any way I can avoid this problem?

Many thanks!

Jez
Jul 20 '05 #1
6 167392
Hi,

Jez wrote:
Hi,

I've created a function which opens a popup window containing a
calendar. When a day is clicked, the date is entered into a text box
on the parent page and the popup is closed.

The link I'm using on the parent page is ...

<a href="#" onClick="popupcal()">choose date</a>

My form is quite long, and when the popup is opened I'm returned to
the top of the page meaning I then have to scroll down again to
continue entering information.

I'm assuming this is due to the href="#" part of the link? Is there
any way I can avoid this problem?

Many thanks!

Jez


To cancel the link's action, you must return false in the ONCLICK event
handler.

Note that it is best to avoid # in the HREF, and instead link to a page
explaining why JavaScript should be enabled for this functionality, or
offering some workaround.

<a href="noJs.html" onClick="popupcal();return false;">choose date</a>

Laurent
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #2
<a href="#" onClick="Dofunction(); return false;">

The browser executes the OnCLick event first, and only follows the href if
the OnClick event/javascript function returns true. If you place 'return
false' in the OnClick the href is ignored. (assuming javascript is enabled
in the browser)

You can do the same sort of thing for form validation

function Validate()
{

if(form.element.value is invalid)
{
alert('element n is invalid.');
form.element.focus();
return false;
}

return true;
}

<form name="myform" method="get" action="process.asp" onSubmit="return
Validate();">

"Jez" <je**********@btinternet.com> wrote in message
news:ad**************************@posting.google.c om...
Hi,

I've created a function which opens a popup window containing a
calendar. When a day is clicked, the date is entered into a text box
on the parent page and the popup is closed.

The link I'm using on the parent page is ...

<a href="#" onClick="popupcal()">choose date</a>

My form is quite long, and when the popup is opened I'm returned to
the top of the page meaning I then have to scroll down again to
continue entering information.

I'm assuming this is due to the href="#" part of the link? Is there
any way I can avoid this problem?

Many thanks!

Jez

Jul 20 '05 #3
> Note that it is best to avoid # in the HREF, and instead link to a page
explaining why JavaScript should be enabled for this functionality, or
offering some workaround.

<a href="noJs.html" onClick="popupcal();return false;">choose date</a>


It is even better to write the url of the file shown in the popup in the
href attribute, so people without Javascript AND searchengines will find the
page, too.

--
Markus
Jul 20 '05 #4
return false; after popupcal(); this will stop the href running.

You could also put href="javascript:popupcal();" but this won't work for non
js users so I'd suggest putting the same page the popup loads in the href,
target="_blank" and return false on the click.

So :-

<a href="page.html" onClick="popupcal('page.html');return false;"
target="_blank">date</a>

Now this is general for all popups. To ensure you cover JS and non JS
people, but don't forget, the text input bit on the parent for non JS people
won't work.

Good luck hope that helps.

Stu

"Jez" <je**********@btinternet.com> wrote in message
news:ad**************************@posting.google.c om...
Hi,

I've created a function which opens a popup window containing a
calendar. When a day is clicked, the date is entered into a text box
on the parent page and the popup is closed.

The link I'm using on the parent page is ...

<a href="#" onClick="popupcal()">choose date</a>

My form is quite long, and when the popup is opened I'm returned to
the top of the page meaning I then have to scroll down again to
continue entering information.

I'm assuming this is due to the href="#" part of the link? Is there
any way I can avoid this problem?

Many thanks!

Jez

Jul 20 '05 #5
Stuart Palmer wrote:
return false; after popupcal(); this will stop the href running.

You could also put href="javascript:popupcal();" but this won't work for non
js users so I'd suggest putting the same page the popup loads in the href,
target="_blank" and return false on the click.

So :-

<a href="page.html" onClick="popupcal('page.html');return false;"
target="_blank">date</a>


Given your structure, I'd go one step further and recommend:

<a href="page.html"
onClick="popupcal(this.href);return false;"
target="_blank">date</a>

Specifically, substituting this.href for page.html. That way, if you ever need
to revise the site structure, you can simply change the HREF, as you would with
an ordinary HTML link.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #6
Jez
Thanks all very much indeed!

Jez
Jul 20 '05 #7

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

Similar topics

1
by: Chris Riesbeck | last post by:
I need to remove colors put on links with Javascript so that CSS hover works on uncolored links. Both background color and transparent turn off the hover effect, and null doesn't remove the color....
10
by: Eric-Sebastien Lachance | last post by:
Hey there, I decided to just create a 100% height and width div that filled the space over a background header image, and add an onclick event to redirect to the my index... Doesn't seem to work...
2
by: Peter | last post by:
Hi, this is the code, and new row and new cell generated ok, but why the onclick and onmouseover doen't work? Thank you in advance! <html> <head> <script language="javascript"> function...
7
by: Nathan | last post by:
I've been reading a bit about proper ways to use <a> tags to launch javascript functions, but I'm still not clear on one issue. I understand it is common to create a link in HTML such as <a...
2
by: Vincent van Beveren | last post by:
Hey everyone, I've looked for this and I wouldn't know what the best practice would be for solving the following problem. We use a BASE tag in our HTML pages. Now we have some links that use...
6
by: kelvlam | last post by:
Hello, I'm a new begininer with JavaScript. I'm trying to figure out which is the best approach, and to understand the differences between them. I have a <Aelement that's suppose to either...
4
by: jodleren | last post by:
Hi all I have a file I open in a smaller window, like this: <a href="#" onclick="window.open.... but it also causes the main window to jump to the top. What have people done to avoid that?...
5
by: dangt85 | last post by:
Hello, I have the following page: ... <style type="text/css"> body { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; }
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.