Aniket Singh
Aniket Singh

Reputation: 877

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

Upvotes: 2

Views: 90

Answers (3)

Marco Scabbiolo
Marco Scabbiolo

Reputation: 7459

Looks like you're not preventing the button to reload the page, which it does in Chrome at least. To prevent this, you have to prevent the default event from firing.

Define your button like this, so you forward the event parameter to you handler:

<button onclick="TwkInvite(event)" class="btn">

Now prevent the default event handling with event.preventDefault()

function TwkInvite(e) {
    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";

    // The event is in window.event in IE
    var e =  e || window.event; 

    // e.prevenDefault() for modern browsers, e.returnValue for IE
    e.preventDefault ? e.preventDefault() : (e.returnValue = false);
}

Upvotes: 1

rrk
rrk

Reputation: 15846

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

Upvotes: 2

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

Looks like a permissions issue.

When I turned this into a snippet, I got this error in the console:

Refused to display 'https://accounts.google.com/o/oauth2/auth?client_id=*********-*************…/app/invite-send&scope=https://www.google.com/m8/feeds/&response_type=code' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.

function TwkInvite() {    
    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";
}
<button onclick="TwkInvite()" class="btn">
    <i class="fa fa-google"></i>Import your Gmail Contacts
</button>

Upvotes: 0

Related Questions