I want to check if the browser opens the popup window. In case the browser denies opening the window, I want to take some other action.
This is my example code:
function open_window(){
window.open("url",_blank);
}
if(!open_window(){
//action
}
If the popup is blocked by browser, window.open will return null. So this function will return false.
function firepopup(url,width,height) {
n=window.open(url,'_blank','toolbar=0,location=0,directories=0,status=1,menubar=0,titlebar=0,scrollbars=1,resizable=1,width='+width+',height='+height);
return n==null;
}
You can try something like this:
var winRef;
var url = 'http://someurl';
winRef = window.open('', 'winPop', 'sampleListOfOptions');
if(winRef == null || winRef.document.location.href != url)
{
winRef = window.open(url, 'winPop')
//Some Another Action
}
Related
Given the method:
function openInNewWindow(url) {
var win = window.open(url, "title");
win.focus();
}
In Safari the object "win" is "undefined" after "window.open" and the new window is not opened.
Any ideas why this happens?
Safari's default popup blocker is more aggressive than other browsers. The method should look like this.
function openInNewWindow(url) {
var win = window.open(url, "title");
if (!win) {
alert("Please disable your popup blocker.");
} else {
win.focus();
}
}
Below is the piece of code I am using to open a link in a new window say "abc".
If the user again clicks on the same link, it should close and reopen the link in the same window "abc".
window.openOrFocus = function(url, "abc") {
if (!window.popups) {
window.popups = {};}
if (window.popups["abc"]){
var v=window.open("", "abc");
v.close();}
window.popups["abc"] = window.open(url, "abc");
}
But Now, say I click on the link, it opens the URL in a new window named "abc".
Now I go and close the window "abc". and go back and again click on the link.
That time it shows up the pop up blocker.
I am confused as to why this pop up blocker is coming when the I go and manually close the window and try to reopen by clicking on the link.
Happens both in IE as well as Chrome
Probably because you're calling window.open with a blank URL or repeatedly in that case.
You don't need your window.open("", "abc") call; instead, just use the window reference you already have:
window.openOrFocus = function(url, windowName) {
if (!window.popups) {
window.popups = {};
}
if (window.popups[windowName]){
window.popups[windowName].close();
}
window.popups[windowName] = window.open(url, windowName);
};
I would also listen for the unload event so you can remove your reference:
window.openOrFocus = function(url, windowName) {
if (!window.popups) {
window.popups = {};
}
if (window.popups[windowName]){
window.popups[windowName].close();
}
window.popups[windowName] = window.open(url, windowName);
window.popups[windowName].onunload = function() {
delete window.popups[windowName];
};
};
Side note: This is a syntax error:
window.openOrFocus = function(url, "abc") {
// --------------------------------^
I've replaced it with windowName in the code above.
I have a servlet that generates a PDF and the output goes to a new browser window.
I am attempting to replace the title of that new window using the updateTitle() function below.
However, when I try to assign the report name (repName) to the window instance, IE11 throws a "Permission denied" error. Any ideas?
function showReport(url, repName){
var repWin = window.open(url);
updateTitle(repWin, repName)
}
function updateTitle(repWin, repName) {
setTimeout(function() {
repWin.document.title = repName; //IE11 console throws PERMISSION DENIED here
}, 3000);
}
You will need to use something like postMessage.
On your original window:
function showReport(url, repName) {
var repWin = window.open(url);
repWin.postMessage('setTitle:' + repName, '*');
}
On the repWin:
function updateTitle(message) {
var m = message.data.split(':'),
eventType = m[0],
data = m[1];
if (message.origin === 'YOUR_URL_HERE' && eventType === 'setTitle' ) {
repWin.document.title = data;
}
}
window.addEventListener("message", updateTitle, false);
Note: This will obviously only work if you can modify the source for the window you are opening.
I need to close the modal dialog with return a value in a same function
I cannot write code as follow,because when a returning a value, next line never be excuted,
function butOK_OnClick() {
return "OK";
window.close();
}
so is this the right way ?
function butOK_OnClick() {
window.returnvalue = "OK";
window.close();
}
or what is the best way to do this ?
Assuming you're using window.showModalDialog to open the window (since window.open does not allow for return values), you'd just set the returnValue property of the modal and then set it to a variable in the opener.
Opener window:
var returnedValue = window.showModalDialog(url);
Modal window:
window.returnValue = 'foo';
window.close();
you can use,
$("#modalId", window.top.document).data("cancelled", true);
$("#modalId", window.top.document).data("returnValue", returnVal);
The following is in the NewForm.aspx which gets executed from a parent page (surveys/lists/testsurvey/allitems.aspx). The javascript works and open google. However, it opens the google in the pop up window (newform.aspx) but i need to close the popup window and show google.com on the parent window (or whatever the parent window link is)
<script type="text/javascript">
function redirect()
{
var inputcCtrls = document.getElementsByTagName("input");
for(m=0; m<inputcCtrls.length; m++)
{
if(inputcCtrls[m].type == 'button' && inputcCtrls[m].value == 'Finish')
{
var funcOnClick = inputcCtrls[m].onclick;
inputcCtrls[m].onclick = function () { window.location = "http://www.google.com/" }; }
}
}
redirect();
</script>
Use the opener property to find the window that opened the current window:
window.opener.location = "http://www.google.com/";
window.close();