Making a function from onload event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ldixon789@yahoo.co.uk

    Making a function from onload event

    I'm trying to convert an on load event into a function. I think this
    should be really simple, but I'm new to Javascript. The following
    works fine as the page loads

    var favorite = GetCookie('Demo Name');

    if (favorite > '1')
    {
    window.location .href = 'page2.htm';
    }
    else
    {
    alert('You must complete the lesson before proceeding');
    }

    I have tried to make it into a function by adding
    function CookieRedirect( ) {
    at the top and a closing } at the end, and calling it using a form
    button in the body:
    <INPUT TYPE="button" VALUE="Redirect " onClick="Cookie Redirect ()">-
    but no luck.

    Any help much appreciated, Laura
  • Evertjan.

    #2
    Re: Making a function from onload event

    ldixon789@yahoo .co.uk wrote on 16 jun 2004 in comp.lang.javas cript:
    [color=blue]
    > var favorite = GetCookie('Demo Name');
    >
    > if (favorite > '1')
    >[/color]

    alert(2<09) // true [numerical compare]

    alert('2'<'09') // false [ascii string compare]

    ?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • ldixon789@yahoo.co.uk

      #3
      Re: Making a function from onload event

      "Evertjan." <exjxw.hannivoo rt@interxnl.net > wrote in message news:<Xns950AA6 B99E52Deejj99@1 94.109.133.29>. ..[color=blue]
      > ldixon789@yahoo .co.uk wrote on 16 jun 2004 in comp.lang.javas cript:
      >[color=green]
      > > var favorite = GetCookie('Demo Name');
      > >
      > > if (favorite > '1')
      > >[/color]
      >
      > alert(2<09) // true [numerical compare]
      >
      > alert('2'<'09') // false [ascii string compare]
      >
      > ?[/color]

      Thanks very much that helped.

      I have a related problem when setting the cookie. I only want to set
      the cookie if the cookie value is less than a form field value. It
      works fine up to number 9, but judges 10 as less than 2! I think it is
      doing an ascii string compare rather than a numerical compare. The
      code I'm using is:

      function SetTheCookie()
      {
      var favourite2 = GetCookie('Page Number');
      if(document.for ms[0].elements[0].value > favourite2)
      {
      var expdate = new Date ();
      expdate.setTime (expdate.getTim e() + (24 * 60 * 60 * 1000 *
      365));
      SetCookie('Page Number', document.forms[0].elements[0].value,
      expdate);
      return false;
      }
      }

      Thanks again,
      Laura

      Comment

      • Evertjan.

        #4
        Re: Making a function from onload event

        ldixon789@yahoo .co.uk wrote on 18 jun 2004 in comp.lang.javas cript:[color=blue]
        > I have a related problem when setting the cookie.[/color]

        A new question should have a new thread.
        Give that tread a meaningful name, regarding the question

        [color=blue]
        > I only want to set
        > the cookie if the cookie value is less than a form field value. It
        > works fine up to number 9, but judges 10 as less than 2! I think it is
        > doing an ascii string compare rather than a numerical compare. The
        > code I'm using is:
        >
        > function SetTheCookie()
        > {
        > var favourite2 = GetCookie('Page Number');
        > if(document.for ms[0].elements[0].value > favourite2)
        > {
        > var expdate = new Date ();
        > expdate.setTime (expdate.getTim e() + (24 * 60 * 60 * 1000 *
        > 365));
        > SetCookie('Page Number', document.forms[0].elements[0].value,
        > expdate);
        > return false;
        > }
        > }[/color]

        SetCookie(
        and
        GetCookie(

        They are unknown javascript functions

        Again you probably are comparing strings, which is only useful if you
        want to sort a series of strings.

        I hope to understand now the why of thre difference I wrote about in:
        [color=blue]
        > alert(2<09) // true [numerical compare]
        >
        > alert('2'<'09') // false [ascii string compare][/color]

        To convert a string to a number, you can but a unary + or 1* in front of
        it.

        myString = '0123'
        myNumber = +myString // will contain the number 123

        So how would you change the below code code?:

        <form><input value='0009'></form>
        <script ...
        var JamesBond = '007'
        if(document.for ms[0].elements[0].value > JamesBond ) alert("yes")
        </ ...

        Please do not try to write complex code. First check the individual
        formulas in a temporary html, like I did in the above alerting example.

        --
        Evertjan.
        The Netherlands.
        (Please change the x'es to dots in my emailaddress)

        Comment

        Working...