This function has an onClick event that calls a function that calls This function

Discussion in 'Javascript' started by Bob, Oct 24, 2006.

  1. Bob

    Bob Guest

    The title says it all. The nextmon() and prevmon() functions are listed
    afterward, since they call fDrawCal().I'm sure you see the
    problem...prevmon() is not defined prior to being mentioned in the
    onClick event. But what's the solution? Here's the structure that I
    have now:

    function fDrawCal(iMonth, iYear) {
    var myMonth;
    myMonth = fBuildCal(iYear, iMonth);
    document.write('<table id="calendar" cellspacing="0" cellpadding="0"
    summary="One month calendar">');
    document.write('<caption><a onClick="prevmon()" title="previous month"
    class="nav">&laquo;&nbsp;</a>'+ThisMonth+' '+iYear+'<a onClick"nextmon"
    title="next month" class="nav">&nbsp;&raquo;</a></caption>');
    document.write(' <tr>');
    ...
    etc.
    ...
    }

    function nextmon() {
    iMonth = iMonth+1;
    if (iMonth==13) {iYear=iYear+1; iMonth=1}
    fDrawCal(iMonth, iYear);
    }

    function prevmon() {
    iMonth = iMonth-1;
    if (iMonth==0) {iYear=iYear-1; iMonth=12}
    fDrawCal(iMonth, iYear);
    }
    Bob, Oct 24, 2006
    #1
    1. Advertising

  2. Bob

    RobG Guest

    Bob wrote:
    > The title says it all.


    No it doesn't.

    > The nextmon() and prevmon() functions are listed
    > afterward, since they call fDrawCal().I'm sure you see the
    > problem...prevmon() is not defined prior to being mentioned in the
    > onClick event.


    That isn't the problem (at least for what you have posted). Your call
    to document.write will completely erase the content of the document,
    the script that is in the page no longer exists when the new page is
    written.


    > But what's the solution?


    Do not use document.write() to build your calendar, use a in-page popup
    (like thousands of calendar pop-ups already do).

    Matt Kruse has a bunch of them here:

    <URL: http://www.mattkruse.com/javascript/calendarpopup/ >


    --
    Rob
    RobG, Oct 24, 2006
    #2
    1. Advertising

  3. Bob

    Bob Guest


    > Do not use document.write() to build your calendar, use a in-page popup
    > (like thousands of calendar pop-ups already do).
    >
    > Matt Kruse has a bunch of them here:
    >
    > <URL: http://www.mattkruse.com/javascript/calendarpopup/ >


    I'm not trying to select a date. CSS controls the position of the
    calendar. I was hoping to have the onClick event create another
    calendar...on top of the initial calendar, on the page - not as a pop
    up.

    In Pascal/Delphi there is a Forward declaration that handles this type
    of organization.

    var Imon = 1;
    function Foo; forward;

    function Bar(Imon) {
    if (Imon <> 12) {Foo();}
    }

    function Foo() {
    Imon = Imon+1;
    Bar(Imon)
    }

    This is somewhat like recursion.

    I was hoping that there is some way in JavaScript to code something
    like this structure.
    Bob, Oct 24, 2006
    #3
  4. Bob

    RobG Guest

    Bob wrote:
    > > Do not use document.write() to build your calendar, use a in-page popup
    > > (like thousands of calendar pop-ups already do).
    > >
    > > Matt Kruse has a bunch of them here:
    > >
    > > <URL: http://www.mattkruse.com/javascript/calendarpopup/ >

    >
    > I'm not trying to select a date. CSS controls the position of the
    > calendar. I was hoping to have the onClick event create another
    > calendar...on top of the initial calendar, on the page - not as a pop
    > up.


    Then just put it in the page inside a div or whatever. Your
    fundamental problem is using document.write() after the page has
    finished loading - it completely replaces the current content of the
    page.


    > In Pascal/Delphi there is a Forward declaration that handles this type
    > of organization.


    It has been over 15 years since I had anything to do with Pascal and
    have never used any version of Delphi. There is no point in explaining
    javascript in terms of code from some other language unless you know
    that I know that language.

    [...]

    > This is somewhat like recursion.


    I can't see how it has any relevance to the problem at hand.

    One of Matt's functions writes a calendar into a div element (the one
    labelled "Default calendar using the DIV-style display"). He uses it as
    a pop-up, to convert it to a stock page element, change the style stuff
    in regard to position and display attributes and there you have it.


    --
    Rob
    RobG, Oct 24, 2006
    #4
  5. Bob

    9.co.uk Guest

    Bob wrote:
    > > Do not use document.write() to build your calendar, use a in-page popup
    > > (like thousands of calendar pop-ups already do).
    > >
    > > Matt Kruse has a bunch of them here:
    > >
    > > <URL: http://www.mattkruse.com/javascript/calendarpopup/ >

    >
    > I'm not trying to select a date. CSS controls the position of the
    > calendar. I was hoping to have the onClick event create another
    > calendar...on top of the initial calendar, on the page - not as a pop
    > up.
    >
    > In Pascal/Delphi there is a Forward declaration that handles this type
    > of organization.
    >
    > var Imon = 1;
    > function Foo; forward;
    >
    > function Bar(Imon) {
    > if (Imon <> 12) {Foo();}
    > }
    >
    > function Foo() {
    > Imon = Imon+1;
    > Bar(Imon)
    > }
    >
    > I was hoping that there is some way in JavaScript to code something
    > like this structure.


    You are misunderstanding the problem - it is not that your code is
    refering to a function that has not yet been defined (javascript
    couldn't care less), it is that document.write replaces the content of
    the current document with the string you send it. Trying to apply
    language concepts from Pascal to Javascript is only going to hurt you,
    as it is here.

    You are wiping out your code completely when you use document.write, so
    the function your onclick handler calls nolonger exists. This is what
    the previous poster was trying to explain to you.

    The solution is not to use document.write (and thus create a new
    document) as was already pointed out, but to replace the calendar
    within the current document. RobG has already given you pointers on how
    to do this.
    9.co.uk, Oct 24, 2006
    #5
  6. Bob

    Bob Guest


    > You are misunderstanding the problem - it is not that your code is
    > refering to a function that has not yet been defined (javascript
    > couldn't care less), it is that document.write replaces the content of
    > the current document with the string you send it. Trying to apply
    > language concepts from Pascal to Javascript is only going to hurt you,
    > as it is here.


    Thanks Rob and N.. I now understand the real problem - the most
    important part to solving any problem.
    Bob, Oct 24, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JerryG
    Replies:
    0
    Views:
    416
    JerryG
    Dec 20, 2005
  2. =?Utf-8?B?QmFobWFu?=

    onclick postback event has disappeared

    =?Utf-8?B?QmFobWFu?=, Mar 2, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    585
    =?Utf-8?B?QmFobWFu?=
    Mar 3, 2006
  3. bob
    Replies:
    3
    Views:
    178
  4. Replies:
    2
    Views:
    242
  5. Replies:
    4
    Views:
    165
Loading...

Share This Page