473,385 Members | 1,806 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.

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 167383
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
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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.