Open and then kill a browser window in a nodejs script? - javascript

So I've been trying to write a script that needs to open a browser window to do google oauth. Now the problem is I can use open to open the browser window, but I can't find a way to close it after the auth happens. It just stays there.

i'm not sure this what your asking, but you can do this by sending the response to the page like code shown below.
router.get('/yourpage',(req, res) => {
res.send("<script>window.close();</script > ")})
so basically when your page gets opened this will send the close script from the server-side to the client by instructing close the window.

Related

How to receive postmessage inside Facebook in-app browser?

I'm trying to send postmessage from the opened window to opener in facebook app browser, but the "opener window" never receives messages. What can be the cause of the problem?
Receiver side:
window.addEventListener('message', function (e) {
window.console.log("on message: " + e.data);
}, false)
Sender side:
window.opener.postMessage('any Message', document.location.origin);
It's hard to tell without seeing more of your code, but as this Opening facebook connect window via javascript? answer states, if you're trying to access the oAuth page, it's not possible.
Show us where you get the variable window.opener, that might add some context.
If you opened it from window.open(/page/), it appears that it is specifically blocked: How do I get around window.opener cross-domain security
as mentioned in that question:
NOTE
Social signups do not work for google, FB, etc within an iframe. I
believe they disallow them for security reasons.
Also from window.opener is null after redirect
window.opener is removed whenever you navigate to a different host
(for security reasons), there is no way around it. The only option
should be doing the payment in a frame if it is possible. The top
document needs to stay on the same host.
But as mentioned in the second answer quoted, instead of using window.opener on the opened page, do everything from the origninal page, and (IF you have access to the source of the popup), make an onmessage on the other page, like mentioned in the accepted answer there, that the correct way to do it is only in reverse:
Do it the other way around. Track the state of the child popup window
from the main (opener) window, and you could easily know when the
child window has been navigated back to you domain, so you could
"talk" to it again. But don't close the child window by itself. Let
the opener window obtain the result from the child window and then
close it.
Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#Example
For example, in your started page, do something like:
var popup = window.open(/*some URL*/);
popup.postMessage("hi");
addEventListener("message", function(e) {
console.log(e);
e.source.postMessage("Hi there"); //official workaround for window.opener on other page
})
then in your "/some URL/" source code page, do something like:
addEventListener("message", function(e) {
console.log(e);
e.source.postMessage("hi back");
});
and just play around with that strategy, but it appears window.opener is out of the picture. Just try console.logging it, it just say null.

javascript link window open without extra slash

Let's say I am currently at the following link:
"localhost/admin/test" when i do
window.open("/user/list/2034", "_blank")
it will appear like this:
localhost/admin/test/user/list/2034
what can i do to make it like this instead?
localhost/user/list/2034
I am assuming that windows should be window. If it really is windows then you have a custom object and need to show us what that is before we can answer your question.
The other point to note is that window.open will open a new window, regardless what you name it, so you don't need to use the _blank name. You only need to specify a name if you want to subsequently reuse that window (e.g. open another URL in the same other window).
When at http://localhost/admin/test, if you do:
window.open("/user/list/2034")
It will go to http://localhost/user/list/2034, unless...
window.open has been redefined somewhere. You can do console.log(window.open) and the console should say something like ƒ open() { [native code] } if it hasn't been redefined.
Your web server is responding to /user/list/2034 with a redirect to /admin/test/user/list/2034. The network tab in your developer console will show you the HTTP requests and responses where you can see if the web server is redirecting.
Your link really doesn't start with a / and you actually have window.open("user/list/2034")
You're viewing a cached version of the page with the above error in it, the source code is fixed but the browser hasn't loaded it. Try again in a private browsing window to see if it still happens.
You have some browser plug-in or extension interfering with your page. Try another browser/computer without the extensions and see if it still happens.

Open a New tab From iframe javascript event without pop up block

I have a iframe in a page.
After i successfully send data to server within iframe, the server responds with next URL to open , i want to open the next URL in a new tab or in the parent window.
The Problem i am facing is, the browser treats it as a popup and blocks it.
In chrome it is treated as pop up and in safari it doesn't do anything.
The iframe is pointing to iframe (written in REACT)
The parent URL is main page.
Here is the part of the code that s causing error.
this.props.dispatch(actions.init_db(vals, (error,data)=>{
this.setState({loading:false})
if(error){
this.setState({error:error});
}else{
url=data.next_url;
var win=window.open(url);
win.focus();
}
}));
Basically actions.init_db() calls an ajax function that writes the value to the server.
The server than responds with error or data.If no errror, the data contains Next URL and i want to open the URL.
Any Suggestion or alternate to this solution is highly appreciated.
You can't. If you call window.open from code that isn't running in direct response to a user action (such as a click), it's likely to get blocked by the popup blocker of the browser. The solution is to make sure you're running that code in direct response to a user action. So for instance, when you have the URL from the server, show a div on the page with the link in it for the user to open themselves.

Never Show Window On Page Launch

I am using C# and asp.net to launch a webpage that I am passing parameters to. That works well! I come from a Windows.Forms background so please forgive me if I am trying to achieve the impossible. What I would like is set the Visibility property of the program (either IE or chrome) to false so the user never sees that a webpage is being launched. I have been using this JS function to close the page, but it seems that the page must completely load before closing which sometimes can take a few seconds.
Does asp.net have the capability to achieve such? And this is my JS code I have been using
string close = #"<script type = 'text/javascript'>
window.returnValue = true;
window.close();
</script>";
base.Response.Write(close);
If you don't want the User to see the page, I assume you just want to post some information to the page. In that case, make an HTTP request via c# code, instead of opening the webpage up in a browser.
On the Project Properties page, Web tab, Start Action section, click the radio button for "Don't open a page. Wait for a request from an external application".

Send signal to web-server on tab close?

Can I force the sending of a 'CLOSE' signal to my server when a tab directed at my website is closed?
(thinking it might be possible with a Javascript alert on close-tab signal)
Hey your looking for the html window attribute onunload so for example < body onunload="javascriptfunction2server()"> will run your javascript function when a tab/window is closed by user (or directed to new page)

Categories