473,379 Members | 1,187 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,379 software developers and data experts.

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('DemoName');

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="CookieRedirect ()">-
but no luck.

Any help much appreciated, Laura
Jul 23 '05 #1
3 1196
ld*******@yahoo.co.uk wrote on 16 jun 2004 in comp.lang.javascript:
var favorite = GetCookie('DemoName');

if (favorite > '1')


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)
Jul 23 '05 #2
"Evertjan." <ex**************@interxnl.net> wrote in message news:<Xn********************@194.109.133.29>...
ld*******@yahoo.co.uk wrote on 16 jun 2004 in comp.lang.javascript:
var favorite = GetCookie('DemoName');

if (favorite > '1')


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

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

?


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('PageNumber');
if(document.forms[0].elements[0].value > favourite2)
{
var expdate = new Date ();
expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 *
365));
SetCookie('PageNumber', document.forms[0].elements[0].value,
expdate);
return false;
}
}

Thanks again,
Laura
Jul 23 '05 #3
ld*******@yahoo.co.uk wrote on 18 jun 2004 in comp.lang.javascript:
I have a related problem when setting the cookie.
A new question should have a new thread.
Give that tread a meaningful name, regarding the question

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('PageNumber');
if(document.forms[0].elements[0].value > favourite2)
{
var expdate = new Date ();
expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 *
365));
SetCookie('PageNumber', document.forms[0].elements[0].value,
expdate);
return false;
}
}
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:
alert(2<09) // true [numerical compare]

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


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.forms[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)
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: brian | last post by:
question about Onload function can one define more than one function for Onload ? <body Onload="function1()"> or can one define multiple functions one after another like <body Onload="f() g()...
7
by: MALdito | last post by:
hi everybody let me say right from the start .. I´m not a coder ... "just" a designer! that said .. here is my question: I´m using dreamweaver´s built in preloader for a menu. it looks like...
11
by: JS | last post by:
I have made a function createFirstMenu where I call "resetMenu" in a JavaScript. But nothing happens when I call resetMenu. function createFirstMenu(sel){ sel = document.getElementById('sel1');...
26
by: johkar | last post by:
I need to cancel the link and execute a function onclick of all the links within the span tag which has a class of "container" assigned. There will be only one span tag with this class applied. ...
7
by: Q | last post by:
Question: why doesn't: window.onLoad = recalculate(); work, and do I have to use: window.onLoad = recalculate; (IE) ???
1
by: jamierphelps | last post by:
I'm sure you guys are tired of seeing this kind of message, but this is hopefully something a little different. I searched first. I am using Drupal for a site and I also want to use Nifty Corners...
7
by: extremerep | last post by:
My task is to change the value of a button and then make it functional with the onClick handler. Changing the value to "Play Again" works, but making the onClick work accordingly does not. The...
2
by: rodchar | last post by:
hey all, i have a button on my web form that after it posts back it suppose to throw up an alert. it works fine but that it throws the alert before the entire page has been displayed. is there any...
4
by: shivendravikramsingh | last post by:
hi friends,i have facing a problem,when i m calling a function on onLoad event of Body,function is not working.but if i call this function on any other event like onblur etc. it work fine.but my...
3
by: Fresno Bob | last post by:
I have a page which is a master page. I want to trigger a Javascript function in the onload event of the body. Simply attaching an onload event to the body which points to javascript function...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.