Ethris
Ethris

Reputation: 65

Javascript window.location does not work

I have problem with this code that I can not understand. Window.location worked perfectly for me before but this time its not and I have no idea. When I access this direction manually - it works, but redirect does not work - just refreshes page. I have no clue what to do !

I've tried to redirect it to google or whatever but it still just refreshes.

$("#remove-form").on("submit", function(event) {
    usunRow();
});

function remove() {
    var id=document.getElementById("id").innerHTML;
    if (id != "ID"){
        $('#remove-modal').modal('show');
    } else $('#error-modal').modal('show');
}

function usunRow(){
    var id=document.getElementById("id").innerHTML;
    if (id != "ID"){
        window.location.href = "usun.php?id="+id;
        alert("usun.php?id="+id);
    } else $('#error-modal').modal('show');
}

here's all functions I am using for this

Upvotes: 1

Views: 3689

Answers (5)

Aswin Prabakaran
Aswin Prabakaran

Reputation: 25

You can try using:

window.location.href = "";

Upvotes: -1

M.Tanzil
M.Tanzil

Reputation: 1995

Change window.location to window.location.href this will changes the window location to a new location.

  1. Make sure your usunRow() function call perfectly and there is no issue in your browser console.
  2. Also make sure your if statement executes, may be your if statement is not executing that will be the reason that your page just refreshes not changing its location.

UPDATED

Add return false, because submitting a form will refresh your page return false will keep the page static and your window.location.href will redirect your page.

$("#remove-form").on("submit", function(event) {
            usunRow();
            return false;
});

Hope this solve your issue.

Upvotes: 1

Barni
Barni

Reputation: 57

How do you invoke usunRow() function? Maybe you have click event on anchor without preventing it. That's why your page is refreshed. You need preventDefault() inside click event.

Upvotes: 0

MattDiMu
MattDiMu

Reputation: 5013

If you open your console and type in window.location, you will understand why it doesn't work. It's an object containing many information about the current location:

Location {hash: "", search: "", pathname: "/questions/38870016/javascript-window-location-does-not-work", port: "", hostname: "stackoverflow.com"…}ancestorOrigins: DOMStringListassign: ()hash: ""host: "stackoverflow.com"hostname: "stackoverflow.com"href: "http://stackoverflow.com/questions/38870016/javascript-window-location-does-not-work"origin: "http://stackoverflow.com"pathname: "/questions/38870016/javascript-window-location-does-not-work"port: ""protocol: "http:"reload: reload()replace: ()search: ""toString: toString()valueOf: valueOf()__proto__: Location

Therefore, you try to replace this object with a string (your target url). The already mentioned property href of this location object is the one you need: window.location.href = "new url" or as short version location.href = "new url"

Upvotes: 0

pid
pid

Reputation: 11607

Use this:

window.location.href = "..."

It's not well documented but window.location actually is an object with non-standard behaviour, such as the reloading of the page and some non-readable or undocumented/quirky/browser-specific fields.

Upvotes: 1

Related Questions