I'm using the code below with window.open to open a new link (same origin) in a new tab. If a tab with the new link is already open, the open tab should be focused instead.
This is all working fine, the focused tab is however always refreshed when it gains focus by this. How can I stop the focused tab from refreshing?
window.open(`./${_id}`, `module_${_id}`);
Check if the window is closed orelse use focus(), this should not refresh the page.
var windowObject = null; // global variable
function openURL(url) {
if(windowObject == null || windowObject.closed) {
windowObject= window.open(url);
} else {
windowObject.focus();
}
}
<button value="Check" onClick="openURL('https://www.google.com')"></button>
Related
I am facing some challenge-
Lets consider I have a menu and under that I have 3 links-
About
Contact Us
FAQs
By default home page is About menu item.
Now when I am clicking on "Contact Us" , I want to open a new tab on same browser window. And without closing this if again I am clinking on "Contact Us" from the first tab, this time I do not want to open new tab because I have already an open tab for Contact us, I just wanted to navigate to the same one which is earlier opened.
How to handle this scenario in jsp.
Is there any way to do this. please help me.
you can always put a name in the target so it will always go to the same window/tab
<a href="somerandomurl.com" target="mywindow">Contact Us<\a>
Its not possible to control this on Internet explorer.
You can however change how IE behaves:
-Internet Options
-Tabs
-Always open pop-ups in a new tab
-Open links from other programs in: A new tab in the current window
target="_blank" will always make sure it opens in a new window
http://www.w3schools.com/tags/att_a_target.asp
It will however refresh the page with the same url
You can have the link return a javascript window object and be able to return to the window anytime you want without refreshing the page
var newtab = null;
function openwindow(){
if (newtab==null) {
newtab = window.open("www.example.com");
} else {
newtab.focus();
}
}
<a onclick="openwindow()">click me</a>
This solution will work if someone refresh the parent page also, it will close the child tab if that tab is already open on refresh of parent page.
Javascript:
<script>
var newtab = null;
window.open('', 'business-list', '').close();
function openwindow() {
if (!newtab || newtab.closed) {
newtab = window.open("/admin/business-list.aspx","business-list");
} else {
newtab.focus();
}
}
</script>
HTML:
<a onclick="openwindow()" style="cursor:pointer">Business List</a>
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
}
I need to open a new tab when a user is scrolling in my website.
The problem is, as far as i understand, that in order for a new tab to open automatically without a pop-up warning from the browsers, the user must first trigger an event in the DOM but apparently onscroll() does not count as one.
Is there a way for me to trigger an even when a user is scrolling in my website that will allow me to open a new tab without the pop-up alert from the browser?
This is currently my code:
window.onscroll = function(ev) {
if(clicked === 1) {
return false;
}
clicked = 1;
var win = window.open('<?php echo $redirect ?>', '_blank');
win.focus();
};
That is native browser functionality and its a good one.
Just imagine if every site could pollute your browser with a number of tabs.
For it to work the user has to accept and allow popups from your domain
In my MVC project I have a button which opens a page in new tab. On button click, the page is opened on new page and the button should be disabled. When the user closes the tab, I want to enable the button. Is there any way I could do this in jquery or javascript? I have searched the web but no answer.
Conceptual idea would be to maintain a reference to the opened window in outer context (module or global variable), so that it can be easily accessed from your code. Here is the example:
var windowsArray = [];
function addWindowInTab() {
var newWindow = window.open('page.html', '_newtab');
windowsArray[windowsArray.length] = newWindow;
newWindow.onunload=enableButton;
}
function removeWindowTab(newWindow){
var idx = array.indexOf(newWindow);
if(idx != -1) {
windowsArray.splice(idx, 1);
}
}
function enableButton(){
$("#myButton").removeAttr("disabled");
}
//somewhere in MVC page
$(function(){
$("#buttonOpenWindow").click(function(){
addWindowInTab();
});
//later, when a button should be disabled...
if (windowsArray.length){
$("#myButton").attr("disabled", "disabled");
}
});
Note also, that here you can perform some more advanced checks, such as if window with particular name is opened or if multiple windows are opened. Bear in mind that in order to open a new tab, the AddWindowInTab function must be called in response to user action, such as pressing Ctrl+ clicking the left mouse button
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');