...

View Full Version : Converting VBScript to Javascript



sarge
07-10-2006, 03:05 PM
Hi-
Because of the limitations of Firefox, I need to convert some VBScripts to Javascripts.
I took a simple example, and put it through a converter, and.. it fell over.
Going through Javascript tutorials, I found that smooth brackets were needed after function, and when I put these in, it moved to the next stage - the curly brackets nightmare.
I've tried every combination of curly brackets that I can think of, but the syntax checker is never satisfied.
I'm frustrated, confused, despairing & angry with Javascript... and life shouldn't be like that. :-(
Could a Javascript guru please cast a quick eye over this?
Thanks,
Brian

====Here's the original VBScript======


<SCRIPT language="VBSCRIPT">
sub document_onclick
Select Case window.event.srcElement.id
Case "terms"
if FrontPage_Form2.terms.value = "OFF" then
FrontPage_Form2.terms.value = "ON"
MSG1.Style.Visibility = "Hidden"
MSG2.Style.Visibility = "Visible"
generate.Style.Visibility = "Visible"
else
FrontPage_Form2.terms.value = "OFF"
MSG1.Style.Visibility = "Visible"
MSG2.Style.Visibility = "Hidden"
generate.Style.Visibility = "Hidden"
end if
Case else
exit sub
End Select
end sub
</SCRIPT>

====Here's the attempted Javascript equivalent...=======


<SCRIPT language="JavaScript">
document.onclick = function(){
switch(window.event.srcElement.id){
Case "terms";
if(FrontPage_Form2.terms.value == "OFF"){
FrontPage_Form2.terms.value = "ON";
MSG1.Style.Visibility = "Hidden";
MSG2.Style.Visibility = "Visible";
generate.Style.Visibility = "Visible";
}else{
FrontPage_Form2.terms.value = "OFF";
MSG1.Style.Visibility = "Visible";
MSG2.Style.Visibility = "Hidden";
generate.Style.Visibility = "Hidden" ;
}
case }else{: {
exit sub;
break;
}
}
}
</SCRIPT>

dswimboy
07-10-2006, 05:08 PM
try this

document.onclick = function() {
switch(window.event.srcElement.id) {
case "terms":
if (FrontPage_Form2.terms.value == "OFF") {
FrontPage_Form2.terms.value = "ON";
MSG1.Style.Visibility = "Hidden";
MSG2.Style.Visibility = "Visible";
generate.Style.Visibility = "Visible";
} else {
FrontPage_Form2.terms.value = "OFF";
MSG1.Style.Visibility = "Visible";
MSG2.Style.Visibility = "Hidden";
generate.Style.Visibility = "Hidden";
}
break;
default:
break;
}
}

i've highlighted the changes i made. hopefully this helps. if not, post the form the script is interacting with as well, so we can test further

glenngv
07-10-2006, 07:24 PM
document.onclick = function(evt) {
if (!evt) evt = window.event;
var el = (evt.srcElement) ? evt.srcElement : evt.target;
var frm = document.FrontPage_Form2;
switch(el.id) {
case "terms":
if (frm.terms.value == "OFF") {
frm.terms.value = "ON";
document.getElementById("MSG1").style.visibility = "hidden";
document.getElementById("MSG2").style.visibility = "visible";
document.getElementById("generate").style.visibility = "visible";
} else {
frm.terms.value = "OFF";
document.getElementById("MSG1").style.visibility = "visible";
document.getElementById("MSG2").style.visibility = "hidden";
document.getElementById("generate").style.visibility = "hidden";
}
break;
default:
break;
}
}

Javascript is case-sensitive. And you have to properly refer to elements like above. IE allows you to directly refer to them using id, but that's not the proper way to do it in standard browsers. And also event handling is a little bit different in IE and Firefox.

BTW, you shouldn't be angry with Javascript. You were just used to VBScript's leniency to ugly codes.

sarge
07-10-2006, 11:47 PM
Many thanks folks. :thumbsup:

dswimboy - that sorted out the syntax OK (...still needed to get the referencing format right). Thanks for that.

glenngv - your code works sweet as a nut!
I'll have to spend some time cross-referencing your code with some Javascript reference books to work out what you've done. ;)

The black cloud has gone, the sun's shining again, I only hate Javascript half as much...

Thanks again-
Blue skies,
Sarge



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum