Lordbug
Lordbug

Reputation: 119

JavaScript window.location.assign() is not working

I am working on an HTML based text-adventure game, where one choice leads to the next, based on which links the user clicks. I am using localStorage to store the HP value of the player, when the HP value hits 0, the game is supposed to end, and the website is supposed to change to a 'Game Over' page. I have two functions that both navigate to a different page, depending on if the HP is above or below a certain number

function alterHP(value) {
    if (typeof (Storage) !== "undefined") {
        var currentHP = parseInt(localStorage.HP);
        localStorage.HP = currentHP + value;
        hp=parseInt(localStorage.HP);
        if (hp<=0) {
            alert(hp); //For debugging, is showing a negative number, like it should be
            /*If the error is syntactical, it is the next line*/    
            window.location.replace('youLose.html');
            /*I used breakpoints and this line IS getting hit*/
            /*It's just not going to the 'youLose' page*/
        }
    }
}

function getResult() {
    var currentHP = parseInt(localStorage.HP);
    if (currentHP > 4) {
        window.location.assign('endAbove4.html');//These both work fine
    } else {
        window.location.assign('endBelow4.html');//These both work fine
    }
}

<li><a
    href="../S9/S9.html" onclick="alterHP(-6);"
    class="ghost-button">Go on to the next scene</a></li>

I have tried every variation of the path name for youLose.html that i can think of, including ../youLose.html, ../../youLose.html, ..\youLose.html, and even putting the full http://gw.mvctc.com/Class2018/smcintosh/Projects/StMarys (I physically opened the page and copied directly from the URL bar to my code, so I know it was the right address.) I also tried using window.location.href="../youLose.html" "youLose.html" '../youLose.html' and 'youLose.html' and window.location.replace()

The most frustrating part is that if you get to the point where getResult(); is called, and you have -26hp, it acts correctly and redirects to the endBelow4.html page. I just need it to redirect to the youLose page as soon as HP hits 0

I cannot for the life of me figure out why it wont redirect to the youLose page.

Upvotes: 0

Views: 1283

Answers (2)

Lordbug
Lordbug

Reputation: 119

Put

window.close('game');
window.open('../youLose.html');

inplace of the window.location.assign(); then in your index.html page place

<script>window.name("game");</script>

and this will close the tab for the game, and then open the youLose.html in a new tab that replaces the original tab.

Upvotes: 0

Ahefaz
Ahefaz

Reputation: 84

use window.open('youLose.html','_self')

Upvotes: 2

Related Questions