JavaScript Pop Up Issue - javascript

This happens in IE only (works in Chrome, Firefox):
The first time I click on the link, it opens the pop up, the second time, it just redirects me to the page.
When I refresh the page, the popup works the first time and then the second time, it again redirects.
function popUpWindow(win) {
myWindow = window.open(win, "popupwin", "height=310,width=570,resizeable=0, menubar=0,toolbar=0,location=0, directories=0,status=0");
}
This is how I call the function
User Status
EDIT: I apologize, the second time, it does not redirect, nothing happens when i click on the link It is redirecting.
EDIT: I close the popup when it opens up the first time and then click the popup link again.
I tried all the solutions presented below, what I found is that the JavaScript method is not called the second time I click on the link.

If the issue is that the popup is being redirected the second time, this is correct behavior according to the code. The name parameter is specified so that a popup can be reused. I don't necessarily recommend this, but you could use this to open multiple popups:
var popupIndex = 0;
function popUpWindow(win) {
myWindow = window.open(win, "popupwin" + popupIndex++, "height=310,width=570,resizeable=0, menubar=0,toolbar=0,location=0, directories=0,status=0");
}

Check whether the window has already been opened or been opened and subsequently closed. The following will focus the pop-up if it exists or open a pop-up otherwise. It's not clear whether you always want a new pop-up or whether focussing an existing one is good enough.
var myWindow = null;
function popUpWindow(win) {
if (!myWindow || myWindow.closed) {
myWindow = window.open(win, "popupwin", "height=310,width=570,resizeable=0, menubar=0,toolbar=0,location=0, directories=0,status=0");
} else {
myWindow.focus();
}
}
If you always want to close an existing pop-up before opening a new one:
var myWindow = null;
function popUpWindow(win) {
if (myWindow && !myWindow.closed) {
myWindow.close();
}
myWindow = window.open(win, "popupwin", "height=310,width=570,resizeable=0, menubar=0,toolbar=0,location=0, directories=0,status=0");
}

I assume you want to refresh the window which you would do like this:
var myWindow = null;
function popUpWindow(win) {
if (myWindow && !myWindow.closed) {
myWindow.close();
}
myWindow = window.open(win, name, options);
}

Related

make only a particular window active until it is closed in js

I have a HTML page which I have opened in a new window. Now I want only that new window to be active until it is closed using close button (cursor should work on the new window only).
I have tried using window.focus() but I don't think it is the right way to do so.
var mywindow = window.open("editparameter.html", "Modify","height=600,width=800,left= 300,top = 100, location = no, modal=yes,alwaysRaised=yes");
mywindow.focus();
This is the code I have written in my current page. Here editparameter.html is the page that opens in the new window.
if you want to block your page or do something else while child tab is opened, you can open new tab and wait its close:
function open() {
// do something with current tab
const tab = window.open('https://stackoverflow.com/');
tab.onclose = onTabClosed;
}
function onTabClosed() {
// revert what has been done
}

How to return focus on parent window from a pop window while loading first time

I am working on a website. Where while loading website first time opening index page with one pop window. First my problem is I want to minimize this pop window automatically while loading first time and focus return to website without any click in website.
Here is my code in index page I write this function:
myFunction();
function myFunction() {
//var myWindow = window.open("music.php", "", "width=320,height=60");
var myWindow = window.open("music.php", "", "directories=0,titlebar=0,toolbar=0,location=0,status=0,menubar=0,scrollbars=no,resizable=no,left=-100000000, top=100000, width=320, height=1, display=none","visibility=hidden");
//myWindow.moveTo(0, 0);
}
And pop window having page extension music.php which should be minimize automatically and focus return to index page without any click on website.
I have already try with set the focus of a popup window to website everytime , but it was also not working.
Well, The window object has an opener property as well.
When we open the browser, that property is null, and when we open a popup , it points to the window the popup has been opened through.
I think something like this should suffice.
myFunction();
function myFunction() {
var myWindow = window.open("music.php", "", "directories=0,titlebar=0,toolbar=0,location=0,status=0,menubar=0,scrollbars=no,resizable=no,left=-100000000, top=100000, width=320, height=1, display=none","visibility=hidden");
myWindow.opener.focus() // can also do window.focus()
}
The execution of JS should not stop, but if it does, you have to add the focus code to your child page/popup page.(Depending on varying browsers)
like
popupHTMLJS.js
focusParent();
function focusParent(){
parent = window.opener;
parent.focus()
}

Window.open only if the window is not open

I have a link on my site that opens a new window to a page that plays a very long audio file. My current script works fine to open the page and not refresh if the link is clicked multiple times. However, when I have moved to a seperate page on my site and click this link again, it reloads. I am aware that when the parent element changes, I will lose my variable and thus I will need to open the window, overiding the existing content. I am trying to find a solution around that. I would prefer not to use a cookie to achieve this, but I will if required.
My script is as follows:
function OpenWindow(){
if(typeof(winRef) == 'undefined' || winRef.closed){
//create new
winRef = window.open('http://samplesite/page','winPop','sampleListOfOptions');
} else {
//give it focus (in case it got burried)
winRef.focus();
}
}
You should first to call winRef = window.open("", "winPopup") without URL - this will return a window, if it exists, without reloading. And only if winRef is null or empty window, then create new window.
Here is my test code:
var winRef;
function OpenWindow()
{
if(typeof(winRef) == 'undefined' || winRef.closed)
{
//create new
var url = 'http://someurl';
winRef = window.open('', 'winPop', 'sampleListOfOptions');
if(winRef == null || winRef.document.location.href != url)
{
winRef = window.open(url, 'winPop');
}
}
else
{
//give it focus (in case it got burried)
winRef.focus();
}
}
It works.
Thanks to Stan and http://ektaraval.blogspot.ca/2011/05/how-to-set-focus-to-child-window.html
My solution creates a breakout pop-up mp3 player that remains active site wide and only refreshes if the window is not open prior to clicking the link button
function OpenWindow(){
var targetWin = window.open('','winPop', 'sample-options');
if(targetWin.location == 'about:blank'){
//create new
targetWin.location.href = 'http://site/megaplayer';
targetWin.focus();
} else {
//give it focus (in case it got burried)
targetWin.focus();
}
}
Like you said, after navigating away from original page you're losing track of what windows you may have opened.
As far as I can tell, there's no way to "regain" reference to that particular window. You may (using cookies, server side session or whatever) know that window was opened already, but you won't ever have a direct access to it from different page (even on the same domain). This kind of communication between already opened windows may be simulated with help of ajax and server side code, that would serve as agent when sharing some information between two windows. It's not an easy nor clean solution however.

how to open in new window

i have another problem :(
this is my script
<script type="text/JavaScript">
function first()
{
var first = confirm("Are you sure you want to navigate away from this page?");
if (first)
window.location = "http://www.yahoo.com.sg"
else
window.close;
}
function second()
{
var second = confirm("Are you sure you want to navigate away from this page?");
if (second)
window.location = "http://www.google.com"
else
window.close;
}
</script>
<p><b>Click the following link to enter yahoo</b></p>
<p>yahoo</p>
<p><b>Click the following link to check google</b></p>
<p>google</p>
its doesnt open in a new window.
please i need some help. any suggestion or recommendation?
Try window.open. This will open a new window:
window.open("http://www.google.com");
See also:
MDN: window.open usability issues
Use the window.open method and specify _blank as the target:
window.open('http://www.yahoo.com.sg', '_blank');
This will open the page in a new window or a new tab, depending on the user settings in the browser.
If you want it to open in a new popup window always, you can specify a parameter string with width and height for the window. However, most browsers will block such a popup that opens a page from a different domain.
If you are opening in a new window, you don't need to check with the user about navigating away.
function first()
{
window.open("http://www.yahoo.com.sg")
}
function second()
{
window.open"http://www.google.com")
}

Javascript: Check for duplicate opened window

Is it possible to check if same window is already opened?
For example i have opened a window via javascript.
Can i check if that's opened on another page via javascript?
Just want to focus on page if it's been opened already to avoid duplicate windows.
Thanks ;)
Look at window.open() method. You must specify the name of the window as the second parameter. If there already is a window with that name, then the new URL will be opened in the already existing window, see http://www.w3schools.com/jsref/met_win_open.asp
If you really want to check, if the window is opened by your own scripts, then you have to keep a reference to the opened window in a global variable or the likes and create it with
var myOpenedWindow = myOpenedWindow || window.open(URL, "MyNewWindow");
You can also encapsulate this behavior in a method:
var myOpenWindow = function(URL) {
var myOpenedWindow = myOpenedWindow || window.open(URL, "MyNewWindow");
myOpenedWindow.location.href= URL;
myOpenedWindow.focus();
}
And call that function with myOpenWindow('http://www.example.com/');
If you have parent--child window then here is a solution that will allow you to check to see if a child window is open from the parent that launched it. This will bring a
focus to the child window without reloading its data:
<script type="text/javascript">
var popWin;
function popPage(url)
{
if (popWin &! popWin.closed && popWin.focus){
popWin.focus();
} else {
popWin = window.open(url,'','width=800,height=600');
}
}
</script>
<a href="http://www.xzy.com"
onclick="popPage(this.href);return false;">link</a>
one more thing ::--- If the user refreshes the parent window, it may loses all its
references to any child windows it may have had open.
Hope this helps and let me know the output.
This will help if you want to open a url from a link
var Win=null;
function newTab(url){
//var Win; // this will hold our opened window
// first check to see if the window already exists
if (Win != null) {
// the window has already been created, but did the user close it?
// if so, then reopen it. Otherwise make it the active window.
if (!Win.closed) {
Win.close();
// return winObj;
}
// otherwise fall through to the code below to re-open the window
}
// if we get here, then the window hasn't been created yet, or it
// was closed by the user.
Win = window.open(url);
return Win;
}
newTab('index.html');

Categories