popup window only 'popping up' once in safari - javascript

I have a JavaScript fired popup window, it's created when hitting the onclick event of a tag:
top.popUp('/bus/popup.asp', '', 'height=457px,width=525px,status=no,scrollbars=no,resizable=yes,location=no,menubar=no,toolbar=no');return false;">
function popUp(url, winName, params, win)
{
var winExists = false;
if (win.handler)
{
if (!(win.handler == "" || win.handler.closed || win.handler.name == undefined))
{
winExists = true;
}
}
if (!winExists)
{
win.handler = window.open(url, winName, params)
}
try
{
win.handler.focus();
}
catch (e)
{
}
return false;
}
I also found these two funky little functions in the main file, fired when the popup window gets unloaded:
// Hack for onUnload and onBeforeUnload
onBeforeUnloadHappened = false;
closeDirectly = false;
function onBeforeUnloadHandler()
{
if (!closeDirectly)
{
openerUnhilightSaveLink();
onBeforeUnloadHappened = true;
}
}
function onUnloadHandler()
{
if (!closeDirectly)
{
if (!onBeforeUnloadHappened)
{
openerUnhilightSaveLink();
}
else
{
onBeforeUnloadHappened = false;
}
}
}
The problem is that in Safari, the window will only popup once, each subsequent click on the link gets you nada, zilch, nothing... how do I correct this?
I did find a couple of articles, Google is my friend, online and they suggested that I add this.close(), which I did to the unload event but it doesn't seem to have solved the problem.
The other thing I find odd is that the onUnloadHandler() doesn't actually happen each time the page is unloaded! Shouldn't this be called each time by the page itself irrespective of what else has happened?!
Thanks

It's my opinion that your popup code isn't written properly. This works in Safari, as I just tested it out. This is what I've done.
// Create a global popup object as you should rarely ever
// have more than one popup open at a time.
window.popUpObj = null;
// The function to call the popup.
function popUp(url, winName, params)
{
// If the window has not been defined or if it has been closes open it.
// Since we defined window.popUpObj to be null we'll expect only 2 conditions.
if(window.popUpObj == null || window.popUpObj.closed)
{
window.popUpObj = window.open(url, winName, params);
}
// Get window focus
window.popUpObj.focus();
// Return false.
// we do this so that when we call it we don't need a ; to return false afterwards
// we just add return before the function call. It's a bit more elegant.
return false;
}
You can call it like this. You should by the way always populate the 2nd parameter with something.
Link
Here's a working HTML doc.
<html>
<head>
<script type="text/javascript">
window.popUpObj = null;
function popUp(url, winName, params)
{
// If the window has not been defined or if it has been closes open it.
if(window.popUpObj == null || window.popUpObj.closed || window.popUpObj.name == undefined)
{
window.popUpObj = window.open(url, winName, params);
}
// Get window focus
window.popUpObj.focus();
// Return false.
return false;
}
</script>
</head>
<body>
Link
close
</body>
</html>

Related

How to get Chrome to throw exception when changing url if it fails

I have a reference to a new window opened with js
var theNewTab="";
theNewTab = window.open(theURL, 'winRef');
then I change the url in the as the user clicks on another link in the parent window using
theNewTab.location.href = targetLink;
theNewTab.focus();
The problem i'm having with chrome that id doesn't throw exception if the the window doesn't exist anymore "closed" unlink FF & IE which im using to open the window again.
try {
theNewTab.location.href = targetLink;
theNewTab.focus();
}catch(err) {
theNewTab = window.open(theURL, 'winRef');
theNewTab.focus();
}
PS: I tried to use "window.open" every time but if the window already open, id does not reload the page or it does but it doesn't re-execute the script I have in document ready I think.
I'm not sure what you need.
<script type="text/javascript">
var theNewTab = null;
function openNewTab(theURL) {
if (theNewTab == null || theNewTab.closed == true) {
theNewTab = window.open(theURL);
} else {
theNewTab.location.href = theURL;
}
theNewTab.focus();
};
// use the function when you need it
$('a').click(function() {
openNewTab($(this).attr('href'));
});
</script>
Is this example helpful for you?

Troubleshoot of beforeunload confirm call

My Code:
//hold window open on form change
window.onbeforeunload = function(e) {
if(formChanges > 0) {
if(formData != initFormData) {
if(confirm('here')) {
e.preventDefault();
}
else {
e = null;
}
}
else {
e = null;
}
}
else {
e = null;
}
};
The three vars (formChanges, formData, and initFormData) are all being filled correctly, and little tests have shown that they are being read correctly within the function. The problem is that the page unloads nomatter what, and no confirmation dialog ever appears.
The console log flashes for a moment before being unloaded (I can't seem to write it's contents to file) and I can see the message Blocked confirm 'here' during beforeunload, but it's gone before I can access it. Any help is appreciated!
WHen using onbeforeunload you have to return a string, like so:
window.onbeforeunload = function(e) {
return 'Dialog text here.';
};
source: https://developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload

Why doesn't this code get the URL from Safari?

I am creating a safari extension. When the user right-clicks on a link in safari, it should bring up the context menu. When the user clicks on "Get URL", it should open the clicked on url in a new window. I can't figure out how to get the url! It always opens "not found" instead.
injected.js :
document.addEventListener('contextmenu', handleContextMenu, false);
function handleContextMenu(event)
{
var target = event.target;
while(target != null && target.nodeType == Node.ELEMENT_NODE && target.nodeName.toLowerCase() != "a")
{
target = target.parentNode;
}
if(target.href)
{
safari.self.tab.setContextMenuEventUserInfo(event, target.href);
}
else
{
var foo = "href not found";
safari.self.tab.setContextMenuEventUserInfo(event, foo);
}
}
Global.html:
<!DOCTYPE HTML>
<script>
var lastUrl;
safari.application.addEventListener("contextmenu",handleContextMenu,false);
safari.application.addEventListener('command', handleCommand, false);
function handleContextMenu(event)
{
var query = event.userInfo;
lastUrl = query;
event.contextMenu.appendContextMenuItem("getUrl", "Get URL");
}
function handleCommand(event)
{
if(event.command === 'getUrl')
{
if (lastUrl)
{
safari.application.openBrowserWindow().activeTab.url = lastUrl;
}
else
{
safari.application.openBrowserWindow().activeTab.url = "not found";
}
}
}
</script>
How do I get the url? It always opens "not found" instead.
Why not just have var last url = event.userInfo in the handleCommand function? The userInfo should be defined at that point, and it should be more predictable that trying to set the value on the contextmenu event.
I don't understand why your code is not working, but there are a couple of things you might want to change anyway.
First, in the injected content script, if there's no target.href, don't bother calling safari.self.tab.setContextMenuEventUserInfo.
Second, in the global script, change your handleContextMenu function as follows:
function handleContextMenu(event) {
if (event.userInfo) {
event.contextMenu.appendContextMenuItem("getUrl", "Get URL");
}
}
That way, if the user didn't right-click a link, the context menu item won't be inserted.
Third, as Matt said, you don't need the lastUrl global variable, unless it serves some other purpose. You can refer to event.userInfo directly in handleCommand. And you don't need to check whether it's empty, because the context menu will only be inserted by handleContextMenu if it's not.
function handleCommand(event) {
if (event.command === 'getUrl') {
safari.application.openBrowserWindow().activeTab.url = event.userInfo;
}
}
Hope this helps.

Check if a popup window is already open before opening it using Jquery

I want to check if a popup window is already open , before I open the popup window.
How do I get it done using Jquery?
Below is my code to open a new popup window :
window.open("mystopchat.php?stat=1&session="+data['myid1']['session_id'][i],"win1","width=500,height=500");
Now before I call this, I want to be sure that this popup window is not already open.
This is a little trick I use, perhaps you could use it:
var winRef; //This holds the reference to your page, to see later it is open or not
function openWindow() {
var url = //Your URL;
if (typeof (winRef) == 'undefined' || winRef.closed) {
//create new, since none is open
winRef = window.open(url, "_blank");
}
else {
try {
winRef.document; //if this throws an exception then we have no access to the child window - probably domain change so we open a new window
}
catch (e) {
winRef = window.open(url, "_blank");
}
//IE doesn't allow focus, so I close it and open a new one
if (navigator.appName == 'Microsoft Internet Explorer') {
winRef.close();
winRef = window.open(url, "_blank");
}
else {
//give it focus for a better user experience
winRef.focus();
}
}
}
Hope it helps.
var popup;
function openPopupOneAtATime() {
if (popup && !popup.closed) {
popup.focus();
/* or do something else, e.g. close the popup or alert a warning */
}
else {
popup = window.open(...);
}
}
var newWindow = null;
function openwindow()
{
// open the new window only if newWindow is null (not opened yet)
// or if it was closed
if ((newWindow == null) || (newWindow.closed))
newWindow = window.open(...);
}
Here is my suggestion:
function authorize(callback) {
if(!document.authorize) {
console.log('Opening authorization window...');
document.authorize = window.open('popup.html','tw','width=300,height=200');
}
if(document.authorize.closed) {
console.log('Authorization window was closed...');
setTimeout(callback,0);
} else {
setTimeout(function(){
console.log('Authorization window still open...');
authorize(callback);
},1000);
}
return false;
}
function test() {
authorize(function(){
alert('teste');
});
}
try this (you will know if the open window was called):
var isOpen = "false";
function OpenPopup()
{
if(isOpen == "false")
{
isOpen = "true";
window.open("mystopchat.php?stat=1&session="+data['myid1']['session_id'] [i],"win1","width=500,height=500");
}
}

jquery/jscript Prevent open multiple popups

I open a popup with the click event of a hyperlink... The popup contains records from a server.
The problem is that when I click rapidly, there are multiple popups at once.
There is a way to prevent this? in which can open a single popup
My code:
$('.wrapper_form a.add').click(function(e)
{
e.preventDefault();
if(typeof(currentPopup) == 'undefined' || currentPopup.closed)
{
url = 'server_page.aspx';
currentPopup = window.open(url,'server','height=500,width=800');
if (window.focus) {currentPopup.focus()}
}
else
{
currentPopup.focus();
}
});
Here is one approach. Not the best solution but it should work. What this code will do is protect against clicking the link a bunch of times and have it open a new instance for each click. This code will not allow the window to be opened more than once in a 1/2 interval, of course you can change the timing.
var hopefullyThisIsNotInGlobalScope = false;
$('.wrapper_form a.add').click(function(e)
{
if (hopefullyThisIsNotInGlobalScope)
{
return false;
}
hopefullyThisIsNotInGlobalScope = true;
setTimeout(function () { hopefullyThisIsNotInGlobalScope = false; }, 500);
e.preventDefault();
if(typeof(currentPopup) == 'undefined' || currentPopup.closed)
{
url = 'server_page.aspx';
currentPopup = window.open(url,'server','height=500,width=800');
if (window.focus) {currentPopup.focus()}
}
else
{
currentPopup.focus();
}
});
Assuming the popup is on the same domain as the window launching it you might be able to replace hopefullyThisIsNotInGlobalScope variable with a global var attached to the window. You can then set that variable when the popup launches and alter it using the browser unload event

Categories