Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logical OR operator in a JavaScript case statement

I have two jQuery files- one [newForms_jQuery.js] being called on userNewItemForm and adminNewItemForm, another [editForms_jQuery.js] being called on userEditItemForm and adminEditItemForm.

I'd like to combine these two files into one jQuery file and I wanted to use a case statement.

I found out that the syntax would be

switch(n)
{
case 1:
  execute code block 1
  break;
case 2:
  execute code block 2
  break;
default:
  code to be executed if n is different from case 1 and 2
}

I am getting the name of the file to differentiate btw New and Edit forms and then doing something based on that. My first thought was to use the logical OR in my case, such as:

case (url="userNewItemForm.aspx") || (url="adminNewItemForm.aspx"):
//do something
case (url="userEditItemForm.aspx") || (url="adminEditItemForm.aspx"):
//do soemthing else

However, then I found https://stackoverflow.com/a/1806279/429694. So, I revised my script to:

switch(url)
{
case "userNewItemForm.aspx": //intentionally left blank to fall through
case "adminNewItemForm.aspx":
// do new record stuff
break;
case "userEditItemForm.aspx": //intentionally left blank to fall through
case "adminEditItemForm.aspx":
//do edit record stuff
break; 
default:
alert(url);
}

This seemed to work, however on further testing, I found that the case statements that did not have break; did not fall through as I'd expected. Any advice?

UPDATE1: Following @jbabey's advice, I revised my script to the following, which I thought solved my problem, however it does not entirely. It seems when I open the url that is in the 2nd part of the OR statement, it doesn't pick up the name, and executes the default case instead.

Here is my current code

    function getFileName() 
    {
    url = document.location.href; //gets the full url. URL is global var after function called
    url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#")); //this removes the anchor at the end, if there is one
    url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?")); //this removes the query after the file name, if there is one
    url = url.substring(url.lastIndexOf("/") + 1, url.length); //this removes everything before the last slash in the path
    return url; //returns filename
    }
$(document).ready(function(){  
     getFileName();
    switch (url)
    {
            case ("eacrfNew_jQuery.aspx" || "newItem_jQuery.aspx"):
              // do new record stuff
            break;
            case ("eacrfEdit_jQuery.aspx" || "editItem_jQuery.aspx"):
              //do edit record stuff 
            break; //close edit record stuff
            default: 
              // which file did it open?    
              alert(url);
    } 
   }); // close doc.ready

FINAL UPDATE:

YOU CANNOT USE LOGICAL OPERATORS IN CASE STATEMENTS Instead, you must use if...else See http://www.naveen.com.au/javascript/jquery/switch-case-in-jquery/319 for details.

function getFileName() 
{
url = document.location.href; //gets the full url into global var
return url; //returns filename
}

function clearID()
{
 //clears field values 
}

$(window).load(function(){  
   $(document).ready(function(){  
     getFileName();
     if ((url === "eacrfNew_jQuery.aspx") || (url === "newItem_jQuery.aspx")) {
        // do new record stuff
     } else if ((url === "eacrfEdit_jQuery.aspx") || (url === "editItem_jQuery.aspx")) {
        // do edit record stuff  
     } else {   
       /// default: which file did it open?
       alert(url);
     }
   }); // close doc.ready 
}); // close window.load 
like image 506
jg100309 Avatar asked Jan 04 '12 19:01

jg100309


1 Answers

That is why i surround with {}

switch(url)
{
   case "userNewItemForm.aspx": 
   case "adminNewItemForm.aspx":
   {
      // do new record stuff
      break;
   }
   case "userEditItemForm.aspx": 
   case "adminEditItemForm.aspx":
   {
      // do edit record stuff
      break;
   }
}

But there is no reason why your current code should not work.

like image 118
Naftali Avatar answered Oct 06 '22 04:10

Naftali