Can not open URL in same window

Hi everyone!

I’m using window.location = “” to link to another html page when a user have entered correctly username and password but it doesn’t work.
I also tried – this work well, but I don’t want it to open a new tab,
window.location.assign(), window.location.href = “” and"" , “_self”) but nothing work.
All I got when I click on login button is the old URL and the question mark behind like this “file:///Users/thotran/Desktop/Programming/HTML/MU-logga-in.html?”
This is my code

$(".login").click(function () {
                    var userID = $(".username").val();
                    var pass = $(".password").val();

                    if (userID == users.login.username && pass == users.login.password){
                        function confirmation() {
                            var answer = confirm("Vill du får mer information om kurserna?")
                            if (answer){
                                // window.location.assign('../HTML/MU-kursinfo.html');
                                // window.location.href = '../HTML/MU-kursinfo.html';
                                // window.location = "../HTML/MU-kursinfo.html"; 
                                // window.location("../HTML/MU-kursinfo.html"); 
                                window.location = ("MU-kursinfo.html");                               
                                window.confirm = function() {};
                                window.alert = function() {};   
                    else if (userID != '' && pass != ''){
                        alert('Fel användarnamn och/eller lösenord! Testa igen!');
                        window.alert = function() {};
                        window.confirm = function() {};     

What am I doing wrong? :thinking:
Grateful for help! :star_struck:

If you use and give the dimension or position properties to the windowFeatures parameter it should work.

// position'MU-kursinfo.html', 'MU-kursinfo', 'top=0, left=0');
// and/or dimension
//'MU-kursinfo.html', 'MU-kursinfo', 'width=600, height=800');

Thanks for the tips! But still, it opened in a new window. I need it to open in the same tab.

Have you tried window.location = "/HTML/MU-kursinfo.html"; without the parentheses?

yes, I did. Didn’t work either :slightly_frowning_face:

Sorry, I somehow totally missed the thread title and just saw this.

but I don’t want it to open a new tab

When you use location.assign() did you have the correct path? What if the files are in the same location and you just use the file name?


Could you specify how exactly it’s not working? Are there any errors in the console when you try it? Have you tried console logging the window object where you call window.location to make sure you still have the correct reference to window?

I just noticed the path you gave in the OP. How are you opening the page?

Try serving the page, you can use the Live Server VS Code extension, or you can use the live-server npm package.

As I describe, if I use window.location(), nothing happen when I click on login button, I got the old URL and the question mark behind like this “file:///Users/thotran/Desktop/Programming/HTML/MU-logga-in.html?”
I did console.log to see what happened but there was noting wrong.
But it works well when I use, that’s weird.

Use ‘_blank’ as second parameter in to open on same tab.

I tried. It opens in a new tab the same as I have it now.