/    Sign up×
Community /Pin to ProfileBookmark

Java not working on some Browsers

I’ve got some javascript that’s not working in Safari (Mac) and Firefox (PC, Mac), but is working in IE (PC) and Opera (PC, Mac). This java is to validate four fields on a form. If the person filling out the form wishes to be contacted by a sales person they have to choose a month, day, time range, and a preferred method. The person filling out the form will get an error message if they want to be contacted to soon. If it’s before noon today then the earliest that we can contact them is tomorrow after noon. If it’s after noon then the earliest we can contact them is in two days. Can anyone tell me why this isn’t working and how to fix it if possible.

[Code]
x=document.forms.Contact
strAlert=””
submitOK=”True”
if(x.drpContactMonth.value==”NA”&&x.drpContactDay.value==”NA”&&x.drpContactTime.value==”NA”&&x.drpContactMethod.value==”NA”){
}
else if(x.drpContactMonth.value==”NA”||x.drpContactDay.value==”NA”||
+ x.drpContactTime.value==”NA”||x.drpContactMethod.value==”NA”){
strAlert=strAlert + “You must fill in all contact fields if you wish to be contacted.” + CRLF()
submitOK=”False”
}
else if(x.drpContactMonth.value!=”NA”&&x.drpContactDay.value!=”NA”&&
+ x.drpContactTime.value!=”NA”&&x.drpContactMethod.value!=”NA”){
var strTodaysDate = new Date()
var strTodaysMonth = strTodaysDate.getMonth()
var strTodaysDay = strTodaysDate.getDate()
var strTodaysYear = strTodaysDate.getFullYear()
var strHour = <% Response.Write strHour %>
var strContactMonth = x.drpContactMonth.value – 1
var strContactDay = x.drpContactDay.value
var strContactYear = strTodaysYear
if(strContactMonth<strTodaysMonth){
strContactYear = strContactYear + 1
}
else if(strContactDay<strTodaysDay&&strContactMonth<strTodaysMonth){
strContactYear = strContactYear + 1
}
else if(strContactDay<strTodaysDay&&strContactMonth==strTodaysMonth){
strContactYear = strContactYear + 1
}
else if(strContactMonth==strTodaysMonth){
if(strContactDay==strTodaysDay&&strHour<12){
if(x.drpContactTime.value==”9am-11am”||x.drpContactTime.value==”11am-1pm”){
strAlert=strAlert + “If you want to be contacted at that time then you will need to give us 2 days or more.” + CRLF()
submitOK=”False”
}
else if(x.drpContactTime.value==”1pm-3pm”||x.drpContactTime.value==”3pm-5pm”||x.drpContactTime.value==”5pm-7pm”||x.drpContactTime.value==”7pm-10pm”){
strAlert=strAlert + “If you want to be contacted at that time then you will need to change the day to tomorrow or later.” + CRLF()
submitOK=”False”
}
}
else if(strContactDay<=strTodaysDay&&strHour>11){
strAlert=strAlert + “If you want to be contacted at that time then you will need to give us 2 days or more.” + CRLF()
submitOK=”False”
}
else if(strContactDay==strTodaysDay+1&&strHour<12){
if(x.drpContactTime.value==”9am-11am”||x.drpContactTime.value==”11am-1pm”){
strAlert=strAlert + “If you want to be contacted at that time then you will need to give us 2 days or more.” + CRLF()
submitOK=”False”
}
}
else if(strContactDay<=strTodaysDay+1&&strHour>11){
strAlert=strAlert + “If you want to be contacted at that time then you will need to give us 2 days or more.” + CRLF()
submitOK=”False”
}
}
[/Code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceMar 29.2006 — Check for error messages. For IE, check the symbol at the left of the status bar. If it is a little yellow triangle with an exclamation point in it, then double click it to see your error messages. For other browsers, open the JavaScript Console to see your error messages.
Copy linkTweet thisAlerts:
@nbcrockettauthorMar 29.2006 — Thanks for the tip. I found the java console in Firefox and Opera, but couldn't figure it out in Safari. Do you know how to access it in Safari? The java console in Firefox says that this one line of code is causing the problem. Can you or anyone offer an alternative string of code?

document.getElementById("txtContactDate").value=document.forms.Contact.drpContactMonth.value + "/" + strContactDay + "/" + strContactYear
Copy linkTweet thisAlerts:
@phpnoviceMar 29.2006 — Generally, speaking, form elements should not be referenced by [B]id[/B]:

document.getElementById("txtContactDate").value

They should be referenced by [b]name[/b] -- as follows:

document.Contact.txtContactDate.value

Then, this looks like a reference to a SELECT element:

document.forms.Contact.drpContactMonth.value

That should be as follows:

sel = document.forms.Contact.drpContactMonth;

alert(sel.options[sel.selectedIndex].value

Lastly, using the [b]with[/b] statement, you can do the whole thing as follows:
with (document.forms.Contact.elements) {
txtContactDate.value = drpContactMonth.options[drpContactMonth.selectedIndex].value
+ "/" + strContactDay + "/" + strContactYear
}
Copy linkTweet thisAlerts:
@felgallMar 29.2006 — Using [b]with[/b] is one way to make the code take three or four times longer to run than if you don't use it.

Why not just set a short one or two character variable name equal to document.forms.Contact.elements instead and reference the fields using that. The code will then run much faster.
Copy linkTweet thisAlerts:
@phpnoviceMar 29.2006 — Using [b]with[/b] is one way to make the code take three or four times longer to run than if you don't use it.[/QUOTE]
Really?!? Why is that? I've been told that using [b]with[/b] is how to make the code run faster -- equivalent to creating a pointer variable (as you mentioned).
Copy linkTweet thisAlerts:
@nbcrockettauthorMar 30.2006 — Thanks guys, I actually figured it out before you had a chance to reply. Here's the code that made it work. Whether or not it runs faster I couldn't tell you. I'm an amatuer at best when it comes to javascript, so my code tends to be simple, which isn't always the best way. I'm still curious about where the java console is in Safari if either of you or anyone else knows. Again thanks for your help!

var x = document.forms.Contact

x.txtContactDate.value=x.drpContactMonth.value + "/" + strContactDay + "/" + strContactYear
×

Success!

Help @nbcrockett spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...