1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

If Then Statement

Discussion in 'JavaScript' started by michael chambers, Aug 22, 2017.

  1. #1
    Help please,

    I want to have a form with two fields one is height one is width the results to execute a java script in an i frame on same page as form.

    The coding i want to use for example in an if then statement is :

    If height = 720 and width = 500 then (statement goes here)

    Once code is set up i want to be able to add further statements for example :

    if height = 720 and width = 500 then (statement 1 goes here)
    if height = 720 and width = 600 then (statement 2 goes here)
    if height = 720 and width = 700 then (statement 3 goes here)

    i can keep adding different statements all run from the height and width in the form input fields
    SEMrush
    Any help would be appreciated in setting up this page including all coding.

    Thank You
     
    michael chambers, Aug 22, 2017 IP
    SEMrush
  2. Blank ™

    Blank ™ Well-Known Member

    Messages:
    224
    Likes Received:
    18
    Best Answers:
    6
    Trophy Points:
    110
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    Just write the code you're initially using, then edit it to add the rest as it comes up. If you mean that you need the format, try

    if((height == 720) && (width == 520)) {
    // do whatever here
    } elseif((height == 720) && (width == 600)) {
    //do it here
    } elseif(...) {
    //whatever
    }

    etc., etc.
     
    Rukbat, Sep 22, 2017 IP
  4. SoftLink

    SoftLink Greenhorn

    Messages:
    27
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    8
    #4
    Is 720 the only height?
    If so, I'd write it like:
    
    function do-width-stuff() {
       fm = document.querySelector("#fmData");
    
       sWidth = fm.sWidth.value;
       sHeight = fm.sHeight.value;
       if(sHeight == "720") {
         switch(sWidth) {
           case "500":
            alert("type code for 500");
            break;
           case "600":
            alert("type code for 600");
            break;
           case "700":
            alert("type code for 700");
            break;
          default:
            alert("Uh oh, now what?");
            break;
         }
       }
    }
    
    Code (markup):
     
    SoftLink, Oct 4, 2017 IP
    deathshadow likes this.
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,438
    Likes Received:
    1,511
    Best Answers:
    220
    Trophy Points:
    515
    #5
    Same here -- don't waste time running the same condition more than once.

    NOT that doing anything based on pixel width has much business being done from JavaScript -- very curious about the usage scenario.
     
    deathshadow, Oct 11, 2017 IP