How to open a popup browser window to contain ongoing updates from the main page?
Can anyone explain to me one or more efficient, reliable methods for opening a popup browser window that will contain an ongoing list of updates from the main browser window?
Imagine that I simply want to display in the popup window one line of text for each mouse click event that happens in the main window. I know how to bind the event handlers, but I don't know how to open an empty popup window, seed it with some content from the main page (e.g. a heading for the window that says, "Events Shown Here!"), and then append HTML content to that window from the main one.
There must be ways of doing this, and I'm sure I could figure it out on my own eventually, but perhaps some smarties from SO can help me more quickly. Google searches on this topic seem to produce an endless list of tutorials and examples on how to open a popup that will contain content from a specified URL, not content provided programmatically from the main page.
There also seems to be a high risk of having the browser block the popup, so any commentary on avoiding that will be appreciated.
If anyone would like to offer comments like "Never use popups! They always suck no matter what," feel free so long as they are not just a flame, because ther开发者_如何学Pythone are obviously certain use cases for which a second window is an appropriate device.
Thanks to everyone in advance, Brian
You can play with this code:
var mywin = window.open("", "my_popup", "location=0,status=0,scrollbars=0,width=500,height=500");
var contents = "<p>HTML content...</p>";
$(mywin.document.body).html(contents);
$(mywin.document.body).append(contents);
Sounds like AJAX to the rescue. Basically, you want to create an AJAX-updatable div in your popup window, and have a JS method poll the server to see if there was an update to user events in the main window. If so, get the new data and refresh the div with it. Then, on the back end, your event handlers or loading script should log the fact that they were called to some session store that forms the basis for the popup data.
Exactly how you'd do this depends on the web framework you're using; nearly all have some sort of AJAX toolkit you can make use of. This is a relatively simple use of AJAX.
As far as getting around popup blockers, using CSS to make the popup appear in-page instead of as a new browser window/tab usually does the trick; you can then implement some GUI element on the popup to collapse it, keeping it out of the way. Or, simply ask your users to configure their blockers to allow your popup. If they trust your site (and it's worthy of trust), no biggie. You could also show the data in a frame, however using HTML frames can be problematic and are all but deprecated in favor of using CSS to define layout using sized divs.
My initial thought is to implement AJAX, on the page and on the "Pop-Up".
Keep a user session, store it in a database. Track the user's actions on the main page, AJAX those actions to the DB. Have a timer set up on the Pop-Up that uses AJAX to pull the records from the DB.
精彩评论