How to make a condition if true open a windows confirm


#1

<script type="text/javascript">
function NewHouse(){
    if(firstHouse >= 1) alert(window.confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")){

        window.location.href='url here';
    }
};
</script>

and yes firstHouse is defined


#2

Not sure it’s a good idea to try to invoke one dialog inside another. They are both modals. Use either alert or confirm, but individually.

alert()

confirm()

Your if statement is not written correctly. There cannot be any code between the conditional and the code block.

You might find that window.location.href does not work in IE or Edge.


#3

Thank you for replying. I tried what you said and got this.

<script type="text/javascript">
function NewHouse(){
    if(firstHouse >= 1){
        window.confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
            window.location.replace("url goes herel");
        };
};
    
</script>

But it still doesn’t work


#4

i am confused by what you want, do you want to know if the user pressed ok or cancel on the confirm dialog?

that is simple, the confirm function returns true (ok button) or false (cancel button), so all we need to do is capture the returned result in a variable, and use it in if/else comparison


#5

Thank your pointing that out. I did what you said got this

<script type="text/javascript">
function NewHouse(){
    if(firstHouse >= 1){
       var Confirm = window.confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
            if(Confirm = true ){
                window.location.replace("url goes here.");
            };
        };
};
    
</script>

But still nothing. What i want to do is have a button that you can buy something (i already have the button and it works) once you buy it the first house var = 1. i want it so that once the first house var = 1 the window confirm will open


#6

why do we only get such a small portion of the code? With the full code, we can help much better, please provide full example, otherwise we just keep going back and forth

Because currently, i can’t see what should trigger the NewHouse function.

also, here:

if(Confirm = true )

a single equal sign means assign, so Confirm gets assigned a value of true, then is evaluated to true, so this condition is always true

then this function which increases first house value, should call NewHouse function, given we are not provided with the full code, difficult to help you with this


#7

I know this isn’t an indentation issue, as JS doesn’t have those, but it can’t hurt to fix the indentation in your code.

Your code currently looks like this:

function NewHouse(){
    if(firstHouse >= 1){
       var Confirm = window.confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
            if(Confirm = true ){
                window.location.replace("url goes here.");
            };
        };
};

Whereas it should look like this:

function NewHouse(){
    if(firstHouse >= 1){
        var Confirm = window.confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
        if(Confirm = true){
            window.location.replace("url goes here.");
        };
    };
};

Again, just to re-iterate, both versions shown will operate in the same way. However, the latter of the two is considered best-practice.


#8

There are six things that still do not follow conventional practice…

  1. Only classes should start with a capital. NewHouse is not a class in the strictest sense; and, neither is Confirm. Both should start with a lowercase letter.
  2. window.confirm is overkill. confirm() works fine since it is a global function.
  3. if (Confirm = true) should be, if (confirmed === true), or better still, if (confirmed).
  4. if does not end with a semi-colon.
  5. Same again, on the outer if statement.
  6. Function declarations do not end with a semi-colon.

#9

I was really only talking about the indentation, but I agree that this:

function newHouse(){
    if(firstHouse >= 1){
        var confirmed = confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
        if(confirmed){
            window.location.replace("url goes here.");
        }
    }
}

is a much better example of best-practice.


#10

Doing that will overwrite the function.

var confirmed = confirm()

In JavaScript the indentation is optional, and only added to improve readability. JS statements are their own miniature program since they are interpreted and JIT compiled one at a time, and executed. We do not need to tell the interpreter how our blocks are nested.


#11

Thank you guys so much for helping me with this. I’m pretty sure what i want to do though is impossible because even after all your guys help it still doesn’t work. Thank again for your feedback. This is the full code.

<p>First House: <span id="firstHouse">0</span><button style=" height: 50px; width: 200px; background-color: red; border-color: #009688;" onclick="buyHouse1()">Buy First house</button> Cost: <span id="House1Cost">100000</span></p>
<script>
var firstHouse = 0;

function buyHouse1(){
    var House1Cost = Math.floor(100000 * Math.pow(1.1,firstHouse))
    if (fruit_sliced >= House1Cost){
    firstHouse = (firstHouse + 1);
    fruit_sliced = fruit_sliced - House1Cost;
    document.getElementById('firstHouse').innerHTML = firstHouse;
    document.getElementById('fruit_sliced').innerHTML = fruit_sliced;
    };
};
</script>
<script type="text/javascript">
function newHouse(){
    if(firstHouse >= 1){
        var confirmed = confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
        if(confirmed){
            window.location.replace("url goes here.");
        }
    }
}
    
</script>

#12

why do you have the function in two different scripts? We can have many functions withing a single script

i seem to have already guessed the cause of the problem correctly, function only execute when called. The newHouse is never called, so it never execute

you could call the newHouse function from the buyHouse1 function, yes, function can call functions


#13

I know this, I specifically said in my original post…

Also…

:man_facepalming:t4: … Oh c’mon! :unamused:

I could have sworn I wrote confirmed… :disappointed_relieved:

:rofl:


#14

Thank you again for the reply i think i did what you suggested but im not sure because this still does not work

<p>First House: <span id="firstHouse">0</span><button style=" height: 50px; width: 200px; background-color: red; border-color: #009688;" onclick="buyHouse1()">Buy First house</button> Cost: <span id="House1Cost">100000</span></p>
<script>
var firstHouse = 0;

function buyHouse1(){
    var House1Cost = Math.floor(100000 * Math.pow(1.1,firstHouse))
    if (fruit_sliced >= House1Cost){
    firstHouse = (firstHouse + 1);
    fruit_sliced = fruit_sliced - House1Cost;
    document.getElementById('firstHouse').innerHTML = firstHouse;
    document.getElementById('fruit_sliced').innerHTML = fruit_sliced;
    };
    function newHouse(){
    if(firstHouse >= 1){
        var confirmed = confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
        if(confirmed){
            window.location.replace("url goes here.");
        }
    }
}
};
    
</script>

#15

well, is suggested this:

which you did, but that was only 50% of my answer, you didn’t implement the other 50%:

which is the bit needed to make it work


#16

Ok but how would i do that i’m very new to functions


#17

a simple google search yields good results:

as a programmer, knowing what is wrong should really allow you to read docs and find solutions on sites like stackoverflow.


#18

Yes finally it worked Thank you guys so much for your help this is how i got it to work.

function buyHouse1(){
    newHouse1();
    var House1Cost =Math.floor(100000 * Math.pow(1.1,firstHouse))
    if (fruit_sliced >= House1Cost){
    firstHouse = (firstHouse + 1);
    fruit_sliced = fruit_sliced - House1Cost;
    document.getElementById('firstHouse').innerHTML = firstHouse;
    document.getElementById('fruit_sliced').innerHTML = fruit_sliced;
    };
function newHouse1(){
    if(firstHouse >= 1){
        var confirmed = confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
        if(confirmed){
            window.location.replace("url goes here.");
        }
    }
}
};

buyHouse1();

Again thank you so much


#19

Did you test this in IE 11 or in Edge? Be sure that location.replace() will work in those browsers.