DotNetRussell
DotNetRussell

Reputation: 9857

Javascript window.location.replace just reloads this page

It should be loading the index.html page with the uname param but for some reason it just keeps reloading this page over and over

I have tried every variation I can think of, to include looking at other working examples of my own and of other people. This is not working because it hates me.

function loginLink() {

    var linkname = getURLParameter("uname");

    window.location.replace("http://www.walkwithpals.com/index.html?uname=" + linkname);
}

This is the html

 <button onclick="loginLink()">Already have an account and want to link to a friend?</button>

Here is the live site and the page in question WalkWithPals

Upvotes: 2

Views: 1976

Answers (2)

icktoofay
icktoofay

Reputation: 128991

The problem is that while you are returning false from your function, you aren't returning false within the event handler. You need to pass on the return value:

<button onclick="return loginLink()">...</button>

However, as Stephen notes in the comments, you really should move away from using inline event handlers. Since I see you've got jQuery included, if you make it easy to identify your button:

<button id="loginButton">...</button>

You can use jQuery to attach to it:

$('#loginButton').click(loginLink);

(such a script should go at the end of the body such that loginButton will exist at that point)

Upvotes: 1

Jivay
Jivay

Reputation: 1034

You can prevent the page from refreshing by making your form return false in order to not reload the page.

<form onSubmit="foo();false;"></form> if the function foo() doesn't alreay return false.

EDIT: Alternatively this looks to be the answer. By adding the attribute type="button" to your button element, this overrides the default submit behaviour.

Upvotes: 3

Related Questions