2

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

DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
  • please don't use inline javascript, there are better and more appropriate ways to handle events. You'll make it a lot easier on yourself in the long run. – Stephen Aug 26 '13 at 01:51
  • possible duplicate of [prevent refresh of page when button inside form clicked](http://stackoverflow.com/questions/7803814/prevent-refresh-of-page-when-button-inside-form-clicked) – kojiro Aug 26 '13 at 01:54

2 Answers2

3

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.

Community
  • 1
  • 1
Jivay
  • 1,034
  • 3
  • 10
  • 23
  • -1 The issue is with the `loginLink()` function, which is not on a submit button. It wouldn't cause the form action to load. – kojiro Aug 26 '13 at 01:47
  • sorry, i didn't know that `button` and `submit` fields don't have the same behavior when clicked. thx =) – Jivay Aug 26 '13 at 01:48
  • @JivayI tried both of your answers and also a combination of them both. None of which worked – DotNetRussell Aug 26 '13 at 01:51
  • I edited my answer with a link i just found. Looks like the `button` element's default behavior is to be a `submit` button, and you can override that by changing your button's type. – Jivay Aug 26 '13 at 01:54
1

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)

Community
  • 1
  • 1
icktoofay
  • 126,289
  • 21
  • 250
  • 231