1

I am using an Instagram API and the only way to log out is through the link https://instagram.com/accounts/logout/ which will redirect the user to the Instagram page (if someone knows an alternative method please let me know). However, I want to redirect the user to my own page. (I had previously tried the methods to do so in this post. The img/iframe will append and redirect the user to my page, but the user was still signed in.) This is the code that I am currently using:

$('.logout').on("click", function () {
     window.open('https://instagram.com/accounts/logout/'); 
     window.close();
     window.location.replace("logout-page.html");
}

The Instagram page successfully opens in a new tab and signs the user out, while my page redirects to the logout page, but I want the Instagram logout page to close on its own after its opens. Other people were saying that window.close() works only when it was opened via script using window.open(), which I am doing and it still does not work. I have also tried window.open("", self).close() and other variations of that and those did not work for me either.

Community
  • 1
  • 1
s.hu
  • 37
  • 1
  • 7
  • "I want the Instagram logout page to close" So, you want close the newly-opened window? Store it in a variable, and use that variable as a context to `close()`. Currently you're trying to close the current window, i.e. the window which executes the script. – Teemu Jul 14 '16 at 16:41
  • I have tried that also and it did not work either – s.hu Jul 14 '16 at 16:42
  • ?? Try with a [delay](https://jsfiddle.net/t4zfe8so/). – Teemu Jul 14 '16 at 16:46
  • The new tab still doesn't close – s.hu Jul 14 '16 at 16:50
  • Do you get it closed on my fiddle? If you don't, please add `console.log(popup)` immediately before the close call. Then check the console, if you'll find `null` or `undefined`, your browser options are blocking the closing. – Teemu Jul 14 '16 at 16:51
  • FYI, the fiddle I've linked above seems to open and close a tab in FF, Edge and IE, but in Chrome it won't be closed. Unfortenately I'm not that familiar with Chrome, that I could say, how to allow it to also close a tab. – Teemu Jul 14 '16 at 17:16

3 Answers3

1

To close the opened window, you have to define it as an object.

A setTimeout delay to close will allow it to fully open the url.
Adjust the delay if needed... But 1 second should work.

myWindow = window.open('https://instagram.com/accounts/logout/');

setTimeout(function(){
    myWindow.close();
},1000);

EDIT
It seems that it is not possible to "hide" a popup...
I found a way is in this answer, but it requires to add a script in the popup page...

With the script above, the popup opens as a new tab... And stays opened only 1 second. That was the best I could tell... Until I found this other solution that you may try.

Community
  • 1
  • 1
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
1

window.open returns an object of the new windows that has been opened. To modify this new window you have to use the returned object like:

new_window = window.open("url");
new_window.close();

Anyway this isn't a good solution because you don't know whether the new opened window has loaded the page successfully or has finished loading it.

I would recommend to just open the logout page in a new window and redirect the current page like this:

my_window = window.open("url", "Window-Name", "height=200,width=200");
document.location = "your_url"; //redirects the current page...`
  • Great, it works! Do you know if there is a way to open the new window behind the original one though? So that the user doesn't see a window suddenly pop up and disappear. – s.hu Jul 14 '16 at 16:56
  • 1
    Yes! read here http://www.w3schools.com/jsref/met_win_open.asp You can position the new window out of the viewport ... Like `left="-1000px", top="-1000px"` – Louys Patrice Bessette Jul 14 '16 at 17:00
  • Actually apparently `left` and `top` are not allowed to be negative. I tried it in my application and it doesn't throw an error, but the window is still visible. Do you know of an alternative method or a way around that? – s.hu Jul 14 '16 at 18:00
  • @user2302052- but in my case i returned document null on that case – Kapil Soni Oct 19 '21 at 12:41
0

just open the url in hidden iframe that is appended in DOM somewhere. Below is jQuery code:

$('#content').append('<div style="display:none"><iframe src="https://instagram.com/accounts/logout/" width="0" height="0"></iframe></div>');

This is how I have implemented in https://www.picosdash.com to logout and allow multiple account login in app, works great.

krisrak
  • 12,882
  • 3
  • 32
  • 46
  • That's what the other post that I linked above suggested, which didn't work for me – s.hu Jul 15 '16 at 16:59
  • It trows this error in console: `Refused to display 'https://www.instagram.com/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.`... Maybe not in all browsers... But in Chrome. – Louys Patrice Bessette Jul 15 '16 at 17:33
  • i know, but it does logout, the error is thrown cause that url later redirects to just "instagram.com" and that is blocked with 'X-frame-Options', but the logout url opens and does the job of logout – krisrak Jul 15 '16 at 18:49
  • Yes there are various successful methods to simply logout by allowing the Instagram redirect. I could also use a brute force method that adds an anchor tag and sets it's href to the the logout link; however, the main point of my question was to redirect to my own page. I have checked/retried the mentioned method several times and with different variations and they have all failed to work. I have solved the issue using the code that Luke posted, but thank you for your input. – s.hu Jul 15 '16 at 23:11