Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
javascript not working in FF...don't know why?
Old 09-24-2007, 09:25 AM javascript not working in FF...don't know why?
Novice Talker

Posts: 7
Name: James Hammond
Trades: 0
Hi guys
I'm a newbie to javascript and have a real problem.

I'm using the checkbox calculator script http://javascript.internet.com/math-related/calculate.js

to use in a seminar registration form. It works fine in IE7, but in FF
nothing happens at all. When I look at the error console it gives the error:

Line 20: Calc is not defined

(line 20 is:

if (calc.item1.checked){
var witem1 = document.calc.item1.value = item1price;
} else {
var witem1 = document.calc.item1.value = 0;
}

I've looked all over the net to find out why FF
doesn't like this. Can anyone help? What should it be changed to?Many
thanks.

James
mollymowler is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-24-2007, 10:24 AM Re: javascript not working in FF...don't know why?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
The script awaits a form named "calc" to be present.
Maybe you don't have it.

Now, some times ago, I've found myself in situations where the form.input notation didn't worked everywhere.
I don't remember if it's in ff or ie or another, but anyway.

A simple way to get through this is to use the dom getElementById(), after that you place an id on the form you want to checks:
Code:
function load(){
  window.status = "Made by Ville Leivo 15.07.2002"
}

function count(){
var item1price = 100;
var item2price = 100;
var item3price = 100;
var item4price = 100;
var calc=document.getElementById('myForm');
  
 // kortti
 if (calc.item1.checked){
   var witem1 = document.calc.item1.value = item1price;
 } else {
   var witem1 = document.calc.item1.value = 0;
 }

 if (calc.item2.checked){
   var witem2 = document.calc.item2.value = item2price;
 } else {
   var witem2 = document.calc.item2.value = 0;
 }

 // www-sivut
 if (calc.item3.checked) {
   var witem3 = document.calc.item3.value = item3price;
 } else {
   var witem3 = document.calc.item3.value = 0;
 }
 if (calc.item4.checked) {
   var witem4 = document.calc.item4.value = item4price;
 } else {
   var witem4 = document.calc.item4.value = 0;
 }
 calc.pay.value = witem1 + witem2 + witem3 + witem4;
}
This code look for a form with an id "myForm".
Try to modify your form, and use this js.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-24-2007, 11:01 AM Re: javascript not working in FF...don't know why?
Novice Talker

Posts: 7
Name: James Hammond
Trades: 0
You're an absolute star! It worked first time, just by adding a form id as you suggested. I have two questions:

1. In your modified script, you have changed the last line from
document.calc.pay.value = witem1 + witem2 + witem3 + witem4;

to calc.pay.value = witem1 + witem2 + witem3 + witem4;

How come it works without this extra 'document' in front of calc? Why did you leave it off?

2. Why does this script need this id identifier? The script was a very popular download for over five years. What has changed in javascript language that means this is now required? I note that http://www.jibbering.com/faq/#FAQ4_41 has a reference to the getElementById(), although I haven't a clue what it's talking about.

Thanks again so much...it's great to have experts around to help newbies out!
mollymowler is offline
Reply With Quote
View Public Profile
 
Old 09-24-2007, 11:50 AM Re: javascript not working in FF...don't know why?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Hehehe...
Thanks.

1:
look at the beginning of count()...
I picked the form via the getelementById.
No need to dig into the document to find it again...

2:
It don't need. Or to be more exact, it would not have needed it 3 years ago.
This script IS VERY OLD AND OUTDATED. 5 years means "elderly" in programming. It relies on the browser translating document.form_name.form_element notation to reaches them.
As I said to you in my first post, I've noticed some times ago that this method seems to be flawed in recent browser.
Maybe it's the fact that the browser being in a strict mode refuse to access the elements that way, or something else, but it don't work 100% of time on every browsers.

So, I simply defined the element to be reached via a DOM function, which is THE way to go in javascript today.
You should never have to do test and different logic between different browser, and DOM function are much more normalized that native browser functions.

Adding an ID to the form is simply the quickest and simplest way to go.
I could have given you a function that would have fetched every forms in your pages, and analyzed them to apply this validation, but it would have been a bit too much, I think.
As the function name says, getelementById() parse the document, and get a "link", or "reference" to the element having that precise id. Nothing more.
But remember that every id must be unique into the page, or else, only the last element rendered by the browser with that id will be taken in account.

If you are curious about DOM and javascript, take a look there:
http://developer.mozilla.org/en/docs...lement#Methods

But if you don't are used to programming, it will mostly looks gibberishes to you
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-24-2007, 12:03 PM Re: javascript not working in FF...don't know why?
Novice Talker

Posts: 7
Name: James Hammond
Trades: 0
Thanks again for this. So, just one more question.
What I have now is

<form name="calc" id="myForm" method="POST" action="">

This retains the 'calc' needed for the script, but places your id in it.
Could I have simply renamed the form 'myForm' then changed all occurrences of 'calc' in the script with myForm and achieved the same thing?

Merci!
mollymowler is offline
Reply With Quote
View Public Profile
 
Old 09-24-2007, 12:13 PM Re: javascript not working in FF...don't know why?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
No.
First, an id is different of an name and second, the method accessed to get the reference to the form are totally different.

The old scripts was trying to address the form via it's name.
Now you access the form via it's id.
Usually, the name and the id are set to the same value, but it's not mandatory, or only to be w3c strict compliant.

But, you could change the id to "calc", and modify the javascript to make a getElementById('calc') in place, and it would work the same. The two have just to stay in sync.

"de rien!"
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to javascript not working in FF...don't know why?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.58203 seconds with 12 queries