How to get data from JS popup window when window is closing - javascript

I'm trying to capture when a form field from a popup window exists when the window is closed. I currently do not have control over the child window, but it is on the same domain as the parent window.
I've looked into using the timer functionality:
var child = window.open(...);
var timer = setInterval(checkChild, 500);
function checkChild() {
if (child.closed) {
<DO SOMETHING HERE>
clearInterval(timer);
}
}
but that won't work for me because I don't think that'll give me access to the child's data.
My popup window has a few form fields for uploading a user's profile image. My question is how would I access whether the user SUBMITTED the form or just simply closed the window? There is javascript in the popup window for the submit button that will handle actually uploading the image, but I would need to somehow capture when they click the "upload" button and be able to see that the user actually had an image to upload submitted.
Thanks in advance!
EDIT:
I went ahead and just looked into the closed window event. I couldn't actually get any data from there, but I was able to read further from the DB on the server-side code to get the data I needed.

You can use the child.onbeforeunload event to get information from the child window before it closes.
As suggested by https://stackoverflow.com/a/15769556/1600851

Related

how to prevent window back button and close tab with javascript

now im doing laravel project. i want to implement javascript code only for preventing the back button and close tab window. i read the article that we can use onbeforeunload(). but this code not suitable with me. because the warning message will display when i want to go to another link menu on my site..
here is my code
window.onbeforeunload = function() {
return "Do you really want to leave?";
};
above code still have problem to me :
i must click at list 1 time on the side. then click close tab, so the pop up will display. if i dont click, the pop up not display and directly close the tab.
im using opera browser but the custom message not display same as what i type. it display "Changes you made may not be saved.". How do i could display the sentence as same as i type?
is there any other code that just prevent only for back button and close the tab? so went i want to navigate to other link menu on my site, the warning message not display.
please help
//you should $event.return = "";
window.addEventListener("beforeunload", function ($event) {
return $event.returnValue = "";
})

Interact between two different web applications using Javascript

I have two applications webApp1 and webApp2. In webApp1 , HTML page contains an tag on click of it an HTML page in webApp2 will be opened in new browser window, page contains one text box and save button. If user enters something in text box and click on save button, need to send a notification from webApp2 to webApp1 so that webApp1 can close the newly opened window and perform some operation after closing window.
How can I achieve this using Java script?
What you are looking for is window.opener.
In Webapp1, window 1, you would have a function in javascript that would need to be done after closing.
function AfterClose() {
// code to execute in Webapp1, triggered in Webapp2
}
In Webapp2, within your save function, do the following.
function Save() {
// ...
// code to save your information
// ...
window.opener.AfterClose();
self.close();
}
Why not just call window.close() as part of the save button click function in webapp2?

Click Anywhere Pop Up Windows Once pervisit?

Anyone can help how to open new windows when clicked anywhere but only once pervisit.
Example, you visit google.com and click anywhere there, and new window will open but only once, second click will not open windows.
Site example : http://afowles.blogspot.com
My script is
Function popup() {
window.open ("http://www.stackoverflow.com","popup","menubar=1,resizable=1,width=450,height=550");
And then put onclick="popup()"
In body.
Code above show new windows every single click on the site. What should i do to make it only show once?
Set a global variable
a=1;
then in function check for variable's value. After windows.open is executed, change the global variable's value, so that windows.open will not be executed again.
Code:
<script>
var a=0;
function popup() {
if(a==0){
window.open ("http://www.stackoverflow.com","popup","menubar=1,resizable=1,width=450,height=550");
a++;
}
}
</script>
Firstly you will need to define to yourself what a visit consists of, i.e. once per day, week, or each time a users lands on your site, every page loaded, etc.
Assuming you want some persistence for your users while they browse, what you need to do on your site is set a condition to be evaluated prior to the first click and if the result indicates a users' first visit then open the page. At the same time, set a value to be checked next time (and most likely all subsequent clicks based on your current implementation). LocalStorage or cookies would be your best options for this.
I would set up something along the lines of:
//check page is loaded
if (cookie.firstVisit) {
//add click event listener
} else {
//set cookie.firstVisit to false
//remove click event listener
}
Instead of spelling out how to do this with cookies here, have a look at this article which explains it all: How to set/unset cookie with jQuery?
Lastly, opening a new window when someone clicks anywhere on your page without them explicitly wanting that action perform is considered bad practice in most scenarios. However, I do not know your situation so this may be the best course of action but thought it was worth mentioning.

PhantomJS executing JavaScript in a popup for data extraction

So I have a web page with some photos of people. When you click on a photo of the person the JavaScript is executed and produces a popup with some more detailed information such as a description etc.
The link for each photo is as follows:
First I want to start with the basics, of just extracting the description etc. from one single person. So I want to execute the JavaScript above to write the popup window, and when I'm on the popup window I can then extract the content of the div's on the popup.
I've looked at PhantomJS and I really don't know where to start. I've used Cheerio to get some simple information from the page, and I want to move on to executing the popup window through JS and then extracting data from that.
Any help would be appreciated as I'm a bit of a newbie to screen scraping in general.
You can do this analogous to how CasperJS does it.
page.open(yourUrl, function(success){
// mainPage is loaded, so every next page could be a popup
page.onPageCreated = function onPageCreated(popupPage) {
popupPage.onLoadFinished = function onLoadFinished() {
popupPage.evaluate(function(){
// do something in popup page context like extracting data
});
};
};
// click to trigger the popup
page.evaluate(function(){
document.querySelector("a.seeMore").click();
// or something from here: http://stackoverflow.com/questions/15739263/phantomjs-click-an-element
});
});
Do not forget to overwrite/nullify page.onPageCreated before navigating away from the main page.

Javascript refresh main page after closing child window (popup)

I have 3 php files: view.php, edit.php and edit2.php.
I use view.php to display content of my DB tables, edit.php to edit a specific row (I use textboxes etc.) and edit2.php to write any changes to my database. After the query is successfully executed, edit2.php uses header("Location: edit.php"); to display the chosen row again (I use session variables in edit.php and unset them on view.php).
edit.php and edit2.php are opened in a small popup window. Now what I would like to have is when I close my small window, view.php should be refreshed.
I tried using onunload but what it does it is triggers itself each time a button is clicked in edit.php to send data to edit2.php, so when it returns to edit.php, it's blank because session variables are unset due to refreshing of view.php.
I have this strange feeling, that I might have explained it in a twisted way... Nevertheless, I would need some help.
What you want to do is open a new window using window.open('edit.php')
Here is the documentation for window.open()
Then watch for when the new window is closed, and refresh the page when that happens. Unfortunately there is no 'on close' event that's fired, but there is a property window.closed that you can watch to see when the window is closed.
So this is what you should do:
var win = window.open('edit.php','Edit Row','width=800,height=600,status=0,toolbar=0');
var timer = setInterval(function() {
if(win.closed) {
clearInterval(timer);
location.reload()
}
}, 1000);

Categories

Resources