I am working on an asp.net web page which has a hyperlink. when ever that hyperlink is clicked, a new browser window is opened using javascript window.open. I want that If user clicks this link multiple times, then only one window is opened and not multiple windows. I just want that window to be highlighted when user clicks that hyperlink multiple times. Do I need to use window.open to detect if the url is opened in any other tab of the browser ? Is there any jQuery plugin built in for this so that I can use it for browser compatibility.
Here is the hyperlink url:
<a onclick="addClick()" href="javascript:void(0)">
New</a>
and here is the code I am using:
function addClick() {
var ID = jQuery("#ID").val();
var PSSWD = jQuery("#PSSWD").val();
var ACCID = jQuery("#ACCID").val();
var PASSWDINT = jQuery("#PASSWDINT").val();
window.open("LoginAPI?ID=" + encodeURIComponent(ID) + "&PSSWD=" + encodeURIComponent(PSSWD) + "&ACCID=" + encodeURIComponent(ACCID) + "&PASSWDINT=" + encodeURIComponent(PASSWDINT) + "", "LoginAPI");
}
Please suggest.
Try
window.open("<url>", "<window name>");
This should always open in the same window. See reference.
HTML:
open window
var wins = {};
function openwindow(){
var url = this.href;
if(typeof wins[url] === 'undefined' || wins[url].closed)
wins[url] = window.open(url);
}
<script>
var windowObjectReference = null; // global variable
function openFFPromotionPopup() {
if(windowObjectReference == null || windowObjectReference.closed)
/* if the pointer to the window object in memory does not exist
or if such pointer exists but the window was closed */
{
windowObjectReference = window.open("http://www.spreadfirefox.com/",
"PromoteFirefoxWindowName", "resizable,scrollbars,status");
/* then create it. The new window will be created and
will be brought on top of any other window. */
}
else
{
windowObjectReference.focus();
/* else the window reference must exist and the window
is not closed; therefore, we can bring it back on top of any other
window with the focus() method. There would be no need to re-create
the window or to reload the referenced resource. */
};
}
</script>
click here
Check the reference https://developer.mozilla.org/en-US/docs/Web/API/window.open
To open only one instance of a popup window in an HTML page, use the windowName parameter of the window.open method.
For example
window.open('http://www.abc.com')
will open a new window each time the user clicks the link containing the window.open code.
In constrast,
window.open('http://www.abc.com','abc')
will open only one instance of the window, no matter how many times users click the link.
you can also use focus function as used below
<script language="javascript" type="text/javascript">
<!--
function popitup(url) {
newwindow=window.open(url,'name','height=200,width=150');
if (window.focus) {newwindow.focus()}
if (!newwindow.closed) {newwindow.focus()}
return false;
}
// -->
</script>
Edit 1
<a onclick="return addClick()" href="javascript:void(0)">New</a>
and here is the code I am using:
function addClick() {
var ID = jQuery("#ID").val();
var PSSWD = jQuery("#PSSWD").val();
var ACCID = jQuery("#ACCID").val();
var PASSWDINT = jQuery("#PASSWDINT").val();
window.open("LoginAPI?ID=" + encodeURIComponent(ID) + "&PSSWD=" + encodeURIComponent(PSSWD) + "&ACCID=" + encodeURIComponent(ACCID) + "&PASSWDINT=" + encodeURIComponent(PASSWDINT) + "", "LoginAPI");
return false;
}
Related
I have tried to close the new window by Window.close() this works for current tab close but I need to close all the new tabs opened by my application
var newwindow=null;
function NewWindow(mypage,myPagename)
{
params = 'width='+screen.width;
params += ', height='+screen.height;
params += ', top=0, left=0';
params +=', scrollbars=yes,menubar=yes,toolbar=yes';
newwindow=window.open(mypage,myPagename, params);
if (window.focus)
{
newwindow.focus()
}
return false;
}
Shall we close the new window by having their name , if yes means how ???
You can assign window.open() to a variable and then addEventListener to the window variable. Something like this:
var testWindow = window.open("enter/your/url");
testWindow.addEventListener('loadstart', yourListenerFunction);
Now when you want to close the window, you need to do this:
testWindow.removeEventListener('loadstart', yourListenerFunction);
testWindow.close();
removeEventListener() method removes the event listener previously registered with addEventListener().
I'd like to open a new window, this window has a list of objects, and these objects should be filtered based on a selection from the previous window. I figured I can filter the list through a function, but how do I run said function?
This is what I am able to do:
var popup = window.open('pageURL');
$(popup.document).ready(function() {
// this is where function should be
popup.alert('HelloWorld');
});
But how do I change the alert to a function?
If I have a function on my other app , function test() { alert('HelloWorld'};
How do I run this function from my first app?
Swapping popup.alert('HelloWorld'); with popup.test(); did not work.
You need the reference to the window opened to call functions in the new window, like:
var oNewWindow = window.open("new.window.url", "mywindow");
oNewWindow.onload = function(){oNewWindow.window.newWindowFunction();};
I ended up with this solution
var popup = window.open('http://s234-0057/actiontracker/SiteAssets/Avvik/html/app.aspx');
var readyStateCheckInterval = setInterval(function() {
if (popup.document.readyState === "complete") {
clearInterval(readyStateCheckInterval);
popup.test();
}
}, 50);
Where I check if the popup window is ready, and when it is, cancel check and run function. Solution is from top answer on this question, by #this.lau_
You can write it like this:
function myFunction(){
alert('HelloWorld');
}
var popup = window.open('pageURL');
$(popup.document).ready(function() {
popup.eval(myFunction + "");
popup.myFunction();
});
myFunction in file that contains this code will run in page with pageURL address.
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 want to open a url in new tab.
I am using following code to open url in new tab.
var url = "/Billing/DownloadEDIForMultipleClaim?month=" + month + "&BillRequestIds=" + billRequestIds.join(',') + "&PatientIDs=" + patientIds.join(',');
window.open(url, '_blank'); //exception here
But this is not working for me it through an exception Uncaught TypeError: Cannot read property 'open' of undefined
I also used following.
var url = "/Billing/DownloadEDIForMultipleClaim?month=" + month + "&BillRequestIds=" + billRequestIds.join(',') + "&PatientIDs=" + patientIds.join(',');
var win = window.open(url, '_blank');
if(win) {
//Browser has allowed it to be opened
win.focus();
} else {
//Broswer has blocked it
alert('Please allow popups for this site');
}
I already seen related questions.
How to open a URL in a new Tab using javascript or jquery?
How to open a link in new tab using javascript
But this is not working for me.
This may be of use:
http://jsfiddle.net/vraCM/12/
it seems that this code snippet contains what you need for this to work.
$(function() {
$("a[href^='http']:not([href*='jsfiddle.net'])").each(function() {
$(this).click(function(event) {
event.preventDefault();
event.stopPropagation();
window.open(this.href, '_blank');
}).addClass('externalLink');
});
});
The js fiddle contains code that can be manipulated to fit your parameters regarding pop-ups within an alert or new window etc.
Also, this article may be worth a read. http://www.jqueryfaqs.com/Articles/Open-new-tab-in-browser-using-jQuery.aspx
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();