1

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

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
Ethris
  • 65
  • 1
  • 10

5 Answers5

1

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.

M.Tanzil
  • 1,987
  • 1
  • 12
  • 28
  • Make sure you `if` executes, are you getting an alert showing your new URL ? – M.Tanzil Aug 10 '16 at 10:22
  • change your `usun.php?id=+id` to `https://www.google.com` to make sure that there is not any problem with `unsun.php` just for checking purpose – M.Tanzil Aug 10 '16 at 10:25
1

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.

pid
  • 11,472
  • 6
  • 34
  • 63
0

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"

MattDiMu
  • 4,873
  • 1
  • 19
  • 29
  • > Though Window.location is a read-only Location object, you can also assign a DOMString to it. This means that you can work with location as if it were a string in most cases: `location = 'http://www.example.com'` is a synonym of `location.href = 'http://www.example.com'`. – pawel Aug 10 '16 at 10:08
0

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.

Barni
  • 14
  • 2
  • My bet is that `usunRow()` is triggered by an event listener on a link or button and there's a `preventDefault` call missing somewhere. This would ecplain the refresh. – pawel Aug 10 '16 at 10:12
-1

You can try using:

window.location.href = "";
UnknownFury
  • 304
  • 1
  • 12