Dynamically changing HREF

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

    Dynamically changing HREF

    I have hyperlink, <a class='navbar'
    href="http://my.calendars.ne t/colchestercamra/">Diary</a>, that points to a
    calendar.

    What I want to be able to do is jump to calendars.net/colchestercamra/#month,
    where I use JavaScript to set up the month.

    Could I use an 'onclick' to set up the properties of the <a> tag just before
    it jumps? I can't use window.location , as this currently remains static
    (using frames I'm afraid!).

    Cheers, Rob.
  • CryingClinton

    #2
    Re: Dynamically changing HREF

    You can have a simply try of ONCLICK to c if the navigation process before
    the onclick event being fired.

    or try: <a href="javascrip t: your_own_naviga te_function();" >...</a>

    --

    "Robert Atkinson" <ratkinson@tb s-ltd.co.uk> ????
    news:ee8fff65.0 407090111.d141e 3a@posting.goog le.com...[color=blue]
    > I have hyperlink, <a class='navbar'
    > href="http://my.calendars.ne t/colchestercamra/">Diary</a>, that points to[/color]
    a[color=blue]
    > calendar.
    >
    > What I want to be able to do is jump to[/color]
    calendars.net/colchestercamra/#month,[color=blue]
    > where I use JavaScript to set up the month.
    >
    > Could I use an 'onclick' to set up the properties of the <a> tag just[/color]
    before[color=blue]
    > it jumps? I can't use window.location , as this currently remains static
    > (using frames I'm afraid!).
    >
    > Cheers, Rob.[/color]


    Comment

    • Richard Cornford

      #3
      Re: Dynamically changing HREF

      Robert Atkinson wrote:[color=blue]
      > I have hyperlink, <a class='navbar'
      > href="http://my.calendars.ne t/colchestercamra/">Diary</a>, that
      > points to a calendar.
      >
      > What I want to be able to do is jump to
      > calendars.net/colchestercamra/#month, where I use JavaScript to set
      > up the month.
      >
      > Could I use an 'onclick' to set up the properties of the <a> tag just
      > before it jumps? I can't use window.location , as this currently
      > remains static (using frames I'm afraid!).[/color]

      A - elements have two properties that can be used by an - onclick -
      handler to set a fragment identifier on the URL that they are about to
      navigate to' the - href - property and the - hash - property. And -
      onclick - handler is executed as a mehtod of the A element so the -
      this - keyword is a reference to that element. As a result a fragment
      identifier may be appended to the URL in the - href - property with:-

      this.href += '#month';

      - or assigned to the - hash - property as:-

      this.hash = '#month';


      Using the - href - is only safe if you know that it will never have a
      fragment identifier already. Using the - hash -property will replace any
      existing (default?) fragment identifier.

      So, with:-

      var mnths = ['January', 'February', 'March', 'April','May',' June',
      'July', 'August', 'September', 'October','Nove mber',
      'December'];

      - defined globally, the A element:-

      <a href="http://example.com/page.html"
      onclick="this.h ash = '#'+mnths[(new Date()).getMont h()];">
      .... </a>

      - would navigate to:-



      - this month, and:-



      - next month (at least when javascript is enabled in the browser.
      Otherwise it would just use the raw - href - attribute (With any default
      fragment identifier it may have.)).

      The - onclick - handler must either not have a return value, or return -
      true - as it does not want to cancel the navigation, having just
      modified the URL that will be navigate to.

      Richard.


      Comment

      • Robert Atkinson

        #4
        Re: Dynamically changing HREF

        Richard - thanks for the reply. This is exactly what I was after.

        Rob.

        *** Sent via Devdex http://www.devdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Mike Foster

          #5
          Re: Dynamically changing HREF

          Richard Cornford wrote:
          [color=blue]
          > ...
          > var mnths = ['January', 'February', 'March', 'April','May',' June',
          > 'July', 'August', 'September', 'October','Nove mber',
          > 'December'];
          >
          > - defined globally, the A element:-
          >
          > <a href="http://example.com/page.html"
          > onclick="this.h ash = '#'+mnths[(new Date()).getMont h()];">
          > ... </a>
          > ...[/color]


          Very nice solution provided by Richard.
          Combine it with the idea presented at the following page
          and make it even better ;-)

          Comment

          Working...