Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-10-2006, 03:05 PM   PM User | #1
sarge
New to the CF scene

 
Join Date: Jul 2006
Location: London
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
sarge is an unknown quantity at this point
Converting VBScript to Javascript

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======

Code:
<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...=======

Code:
<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>
sarge is offline   Reply With Quote
Old 07-10-2006, 05:08 PM   PM User | #2
dswimboy
Regular Coder

 
dswimboy's Avatar
 
Join Date: Nov 2003
Location: mostly in Ann Arbor
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
dswimboy is an unknown quantity at this point
try this
Code:
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
__________________
"There is more than one way to do it."
dswimboy is offline   Reply With Quote
Old 07-10-2006, 07:24 PM   PM User | #3
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Code:
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.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app

Last edited by glenngv; 07-10-2006 at 07:34 PM..
glenngv is offline   Reply With Quote
Old 07-10-2006, 11:47 PM   PM User | #4
sarge
New to the CF scene

 
Join Date: Jul 2006
Location: London
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
sarge is an unknown quantity at this point
Many thanks folks.

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
sarge is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 05:22 PM.


Advertisement
Log in to turn off these ads.