Note that there are some explanatory texts on larger screens.

plurals
  1. POLogical OR operator in a JavaScript case statement
    primarykey
    data
    text
    <p>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. </p> <p>I'd like to combine these two files into one jQuery file and I wanted to use a case statement. </p> <p>I found out that the syntax would be </p> <pre><code>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 } </code></pre> <p>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:</p> <pre><code>case (url="userNewItemForm.aspx") || (url="adminNewItemForm.aspx"): //do something case (url="userEditItemForm.aspx") || (url="adminEditItemForm.aspx"): //do soemthing else </code></pre> <p>However, then I found <a href="https://stackoverflow.com/a/1806279/429694">https://stackoverflow.com/a/1806279/429694</a>. So, I revised my script to:</p> <pre><code>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); } </code></pre> <p>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?</p> <p>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.</p> <p>Here is my current code</p> <pre><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 </code></pre> <p>FINAL UPDATE:</p> <p><strong>YOU CANNOT USE LOGICAL OPERATORS IN CASE STATEMENTS</strong> Instead, you must use if...else See <a href="http://www.naveen.com.au/javascript/jquery/switch-case-in-jquery/319" rel="nofollow noreferrer">http://www.naveen.com.au/javascript/jquery/switch-case-in-jquery/319</a> for details.</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload