onClick and href="#"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jez

    onClick and href="#"

    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="popupc al()">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
  • Laurent Bugnion, GalaSoft

    #2
    Re: onClick and href=&quot;#&qu ot;

    Hi,

    Jez wrote:[color=blue]
    > 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="popupc al()">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[/color]

    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="popupc al();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

    Comment

    • Richard Hockey

      #3
      Re: onClick and href=&quot;#&qu ot;

      <a href="#" onClick="Dofunc tion(); 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.fo cus();
      return false;
      }

      return true;
      }

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

      "Jez" <jez.hailwood@b tinternet.com> wrote in message
      news:ad15f8ee.0 307170129.42230 273@posting.goo gle.com...[color=blue]
      > 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="popupc al()">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[/color]


      Comment

      • Markus Ernst

        #4
        Re: onClick and href=&quot;#&qu ot;

        > Note that it is best to avoid # in the HREF, and instead link to a page[color=blue]
        > explaining why JavaScript should be enabled for this functionality, or
        > offering some workaround.
        >
        > <a href="noJs.html " onClick="popupc al();return false;">choose date</a>[/color]

        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


        Comment

        • Stuart Palmer

          #5
          Re: onClick and href=&quot;#&qu ot;

          return false; after popupcal(); this will stop the href running.

          You could also put href="javascrip t: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="popupc al('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" <jez.hailwood@b tinternet.com> wrote in message
          news:ad15f8ee.0 307170129.42230 273@posting.goo gle.com...[color=blue]
          > 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="popupc al()">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[/color]


          Comment

          • Grant Wagner

            #6
            Re: onClick and href=&quot;#&qu ot;

            Stuart Palmer wrote:
            [color=blue]
            > return false; after popupcal(); this will stop the href running.
            >
            > You could also put href="javascrip t: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="popupc al('page.html') ;return false;"
            > target="_blank" >date</a>[/color]

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

            <a href="page.html "
            onClick="popupc al(this.href);r eturn 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 <gwagner@agrico reunited.com>

            * Client-side Javascript and Netscape 4 DOM Reference available at:
            *


            * Internet Explorer DOM Reference available at:
            *
            Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


            * 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


            Comment

            • Jez

              #7
              Re: onClick and href=&quot;#&qu ot;

              Thanks all very much indeed!

              Jez

              Comment

              Working...