I have openned my application in a Internet explorer tab say "TAB1" , and by clicking a button say "button1" in the "TAB1" , a pop-up say "POP-UP1" will open, in the "POP-UP1" screen if i click a button, it will add some elements in the "TAB1"screen (Parent screen 1) .
Now i open my same application in the another tab say "TAB2" in the same IE browser window and click the "button1" which opens the pop-up in the Same
"POP-UP1" (because the window.open() function code is same with the same pop-up name). Now when i try to add some elements in the "TAB2" (parent screen 2), But instead of adding to "TAB2", it adds the elements in the "TAB1" screen.
How to write the javascript code to add elements in the correct parent screen from whichever the pop-up is initiated. I want the same pop-up to work for all the tabs openned instead of openning many pop-ups from different tabs.
Please help me.
I think is because the name of the popped window is the same:
the parent.opener of the popup is the first tab... so it will always refer to it.
All I can think of is to have a different popup-name
for each tab the site is opened.
This way you will have 2(or more) popups and each will refer to his own parent.oopener.
try this :
var myTime=new Date().getTime();
var popupName="mypopup_"+myTime;
var myPopup=null;
function popMypopup(URL){
myPopup=window.open(URL,popupName,"height=50,width=50");
myPopup.focus();
}
to pop the window use
popMypopup('your/url/here.html')
You can try to see if the parent opener of the popup refers to the current window... if it doesn't close it and reopen it.
Something like this:
var myPopup=null;
function popMypopup(URL){
myPopup=window.open(URL,"mypopup","height=50,width=50");
myPopup.focus();
if(myPopup.parent.opener!=window){
myPopup.close();
popMypopup(URL);
}
}
PS: only tested on IE ... but I believe it should work on all browsers.
Related
I have a web page with a link that opens another page in a new tab. In the newly opened page, I have a link that, when clicked, should bring focus to the original tab.
The new tab is opened via the target attribute:
new tab
I thought this should work:
switch tabs
$('.back-link').click(function() {
if(window.opener) {
console.log('switching tabs');
window.opener.focus();
}
});
The console.log fires, but focus stays on the current tab. What am I doing wrong?
Could you try adding the following event listener to your new tab button instead of target="_blank" :
$('.new-tab-button').click(function(e) {
e.preventDefault();
window.open('/new-page/');
});
This way you actually open a new window instead of new tab, according to the stackoverflow thread linked below, for security reasons, it seems to only be possible to focus to (and maybe from) windows/pop-ups. I'm not sure if what you're trying to do is possible but if so this could be it.
How to change browser focus from one tab to another
From my webpage, I am opening a new page in different tab. When the page in different tab will be loaded, I want to close my webpage i.e. Suppose I am on pageA and I opened pageB using window.open(). Now, when pageB will be opened, I want to close pageA. I tried this jsFiddle -
function onClickBtn()
{
var win = window.open('http://www.google.com','_blank','');
setTimeout(function () {
win.close();
}, 5000);
};
This is my HTML markup -
<input id="btn1" type="button" value="Click me" onclick="onClickBtn()"/>
However, this code is closing pageB ,not PageA.
I have tried a jsFiddle https://jsfiddle.net/gdso9eeg/
Please suggest me a suitable solution.
It is doing what it should do.
You opened the new window with that variable and on using close() with it, it is closing the window which it opened.
If you want to close the parent then open the new page on the parent.
Try this:
In place of _blank put _parent
If page is redirecting on the same page window then window.close will not work due to some browser security for this scenario type we need to use one of the following way.
1.window.history.back();
2.document.referrer
3.Request.UrlReferrer server side
The .close() should follow Javascript's window, not win.
Try
window.close();
Good luck!
EDIT:
Have you checked this post window.close and self.close do not close the window in Chrome ?
"..javascript must not be allowed to close a window that was not opened by that same javascript."
It also offers some workarounds to the issue.
I have been facing a problem.I am able to open a window using window.open method.If I specify the height and width of the window,it opens as a pop up window.If no parameters is given for height or width,then it opens in a new tab.
Is there any property through which I can determine window opened was a pop up or a new tab?
Thank you
Malcolm X
Edit: I have been looking into this a little further.
Seems like there is no different "type" on these windows, simply different options.
A way I found to check if it was a tab or window is to check window.menubar.visible.
For the tab, which is a full and normal window it is true, and for the pop-up the menu is hidden and therefore false. Same applies to window.toolbar.visible.
Works in FF and Chrome at least. Unfortunately not in IE. (Testing done in IE8, which is the version I have installed. For testing of course..)
Example:
if(window.menubar.visible) {
//Tab
} else {
//"Child" Window
}
Found this thread: Internet Explorer 8 JS Error: 'window.toolbar.visible' is null or not an object
If you specify width and height, it means that you also have to specify the name parameter. This can be used in the same way target in an a tag is used, and defaults to _blank.
If you do not specify width and height I assume you also don't specify name and therefore it is opened with name=_blank, which means a new Tab.
If you specify width and height, are you setting a custom name? Doing so results in a child window. If you specify a name, or empty string as name, I suggest you try name:_blank if you want it to be a new tab.
If the window was opened with a name, you can always the window.parent from the child window. If you open with _blank I am not sure if you can get the window.parent
w3schools Window Open
I'm not quite sure what you mean in your question but from what I understand, you might want to use the HTML target attribute:
_blank Opens the linked document in a new window or tab
_self Opens the linked document in the same frame as it was clicked (this is default)
_parent Opens the linked document in the parent frame
_top Opens the linked document in the full body of the window
framename Opens the linked document in a named frame
Source: http://www.w3schools.com/tags/att_a_target.asp
You can detect that using onblur, by checking whether the focus is missed or not
<html>
<head>
<script>
function newTab() {
document.getElementById("demo").innerHTML = "New tab opened!<br><br>refesh this page to recheck ";
}
window.onblur = newTab;
</script>
</head>
<body>
<div id="demo">
Open a new tab and then check this page
</div>
</body>
</html>
Below is my code for displaying a pop up window when the user clicks on an image link:
Javasript
function plusbutton(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=800,height=550,scrollbars=yes');
return false;
}
HTML
<a href="previousquestions.php" onclick="return plusbutton(this, 'previousquestions');">
<image src="Images/plussign.jpg" width="30" height="30" alt="Look Up Previous Question" class="plusimage"/>
</a>
My question is that when the user opens up the window, then when the user clicks away from the window, the window minimizes. I don't want this to happen. If the user clicks away from the window, then it should still display the window.
It is like when you click on "Save As" on Microsfot Word, when the "Save As" window appears, if you click of it, the window still appears, stating that you must either Save the document or Cancel the Save As before being able to do anything else on the document. I want the same this to happen with the pop up window above, how can this be achieved?
Thanks
What you are trying to do is called a "Modal Window", and it is not possible to achieve this goal with a browser window. In fact, you can not even guarantee that it's a window: some browsers would in some cases just open a new tab.
What you should do is open a window with pure HTML within you application. Some librarie allow you to do this easily: jQuery UI dialog for example. Check out http://jqueryui.com/demos/dialog/#modal
If all your architecture bounds your popup content to be delivered by a separate URL, you can do one of the following things:
Either put and iframe in the popup (easy but dirty)
Or retrieve the HTML content with an XMLHttpRequest and use it to define the content of the popup. Maybe a little more tricky, but cleaner.
You should display a modal div, it look like a windows, but it don't dismiss when you click out
if you can use a javascript lib like jQuery, you can do it this way :
$('yourdivselector').dialog({modal : true});
with the modal option set to true
http://docs.jquery.com/UI/Dialog
I have a window I'm opening with a Javascript function:
function newwindow()
{
window.open('link.html','','width=,height=,resizable=no');
}
I need it that once the new window opens that the focus returns to the original window.
How can I do that?
And where do I put the code - in the new window, or the old one?
Thanks!
This is known as a 'pop-under' (and is generally frowned upon... but I digress).. It should give you plenty to google about
You probably want to do something like:
var popup = window.open(...);
popup.blur();
window.focus();
Which should set the focus back to the original window (untested - pinched from google). Some browsers might block this technique.
After calling window.open, you may try to use
window.resizeTo(0,0);
window.moveTo(0,window.screen.availHeight+10);
this way can not really open window in background, but works in similar way. Chrome works fine, did not try other browser.
If Albert's solution doesn't work for you and you actually want the window visible, but to be opened behind the current window, you can try opening a new tab in the opener window and closing it right away, this will bring the focus back to the opener window.
window.open('link.html','','width=,height=,resizable=no');
window.open().close();
However, I believe whether the second window opens in a tab or a new window depends on your browser settings.
Please don't use "pop-unders" for evil.
You can use either
"blur" or
"focus" to do that required action.
"blur"
function newwindow()
{
var myChild= window.open('link.html','','width=,height=,resizable=no');
myChild.blur();
}
"focus"
function newwindow()
{
window.open('link.html','','width=,height=,resizable=no');
window.focus();
}
Put the code in your parentWindow (i.e. the window in which you are now)
Both will work.
tl;dr - in 2022 - ctrl/cmd clicking on a button and window.open(url, "_blank") in a javascript button handler's for loop will open multiple tabs in the background in Chrome.
I'm looking for this as of 2022 and none of the answers here worked (here and everywhere else I looked). My use case is clicking a button in a (progressive) web app which opens deep links to items in a list in background tabs (i.e. not "for evil").
It never occurred to me that ctrl/cmd + clicking on the button would open tabs in the background, but it does just as if the user clicked on an anchor tag itself directly - but only in Chrome. Combined with Chrome's relatively recent tab grouping feature, this can be very useful inside PWAs.
const isMozilla =
window?.navigator?.userAgent?.toString().toLowerCase().includes('firefox') ?? false;
for (let index = 0; index < urls.length; index++) {
const url = isMozilla ? urls.reverse()[index] : urls[index];
window.open(url, "_blank");
}
Note: I reverse() the array on Mozilla to get the order of newly created tabs as the user would expect them.
You can just use '_self'. It will be stay to the same page an
window.open(url, '_self');