Sagar Singh Sagar Singh - 2 years ago 68
Javascript Question

Not able to redirect users using javascript

I have a page where user will click on it to send invites to his friend on gmail

here is the button

<button onclick="TwkInvite()" class="btn">
<i class="fa fa-google"></i>Import your Gmail Contacts

here is the javascript part

function TwkInvite() {
alert('function is called');
window.location.href = "*********-*************";

I have tried window.location.href, window.location, location.href everything

This function is being executed perfectly as alert is also executed but i don't know why it doesn't redirect to google website, whenever i click on button the page get refreshed

No error is printed, i checked mozilla console also but no error found

Answer Source

Your question says that there is no error in the console, that means what ever is causing redirect is a default action. It could be submitting a form for all we know. This will prevent parent form getting submitted, if there is any.

You can use return false to prevent that from happening.

<button onclick="TwkInvite();return false;" class="btn">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download