Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Location
    London
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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."

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Jul 2006
    Location
    London
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •