Sagar Singh Sagar Singh - 6 months ago 9
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
</button>


here is the javascript part

function TwkInvite() {
alert('function is called');
window.location.href = "https://accounts.google.com/o/oauth2/auth?client_id=*********-*************.apps.googleusercontent.com&redirect_uri=https://www.example.com/app/invite-send&scope=https://www.google.com/m8/feeds/&response_type=code";
}


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

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">