473,666 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 23 '05 #1
3 1214
ld*******@yahoo .co.uk wrote on 16 jun 2004 in comp.lang.javas cript:
var favorite = GetCookie('Demo Name');

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******* *************@1 94.109.133.29>. ..
ld*******@yahoo .co.uk wrote on 16 jun 2004 in comp.lang.javas cript:
var favorite = GetCookie('Demo Name');

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('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
Jul 23 '05 #3
ld*******@yahoo .co.uk wrote on 18 jun 2004 in comp.lang.javas cript:
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('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;
}
}
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.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)
Jul 23 '05 #4

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

Similar topics

2
8135
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() h()" ??? is there an equivalent function like sleep() in javascript ? so that one can delay executions of statements via the sleep ?
7
3649
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 this: function MM_preloadImages() { //v3.0 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array(); var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length;
11
1763
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'); resetMenu(sel); } function resetMenu(sel){ sel.length = 0;
26
8076
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. I know you can get a specific tag using document.getElementsByTagName('span'), but I am unsure how to get the one with the class="container". I know there is a getAttribute method, just need a pointer or two to put it all together. Once I know...
7
2066
by: Q | last post by:
Question: why doesn't: window.onLoad = recalculate(); work, and do I have to use: window.onLoad = recalculate; (IE) ???
1
2011
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 Cube for styling purposes. On certain pages, Drupal adds its own onload functions, so I am using the workaround built into NCC, the NiftyLoad=function() method. It seems that the onload event is not getting executed in Firefox Mac (the only...
7
2789
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 following is a snippet of the script. What is keeping it from working? function displaycards1(){ document.form1.reveal1.value="Play Again"; document.form1.reveal1.onClick="setcards();"; } </script>
2
1177
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 way i can get it to display the alert after the entire page is displayed? thanks, rodchar
4
2270
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 requirement is function called first and last time untill i reload the page.so it is only posssible if i use onload event.but it's not working with it.so pls help me,how can i do this.
3
6942
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 causes a javascript error (Object not found on line 0). I read that ASP.net does not like you doing it this way so I was looking at the ClientScript.RegisterClientScriptBlock. This doesn't write anything. I have tried putting it in the page load event...
0
8449
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8360
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8556
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8642
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.