Popup Window will not Close - javascript

I want to close a popup window from an error page. The process is:
Submit a form
At form submission, watch a popup window open
If an error is thrown after form submission, then view the page (not the popup) redirect to an error page
At this time, the popup window should close.
Given the timing of the opening of the popup window and the submission of the form, there isn't an easy way to wait and see if an error is thrown and then decide whether or not to open a popup window. The specific error that I'm programming against is thrown after the opening of the popup window, so I believe the popup window should be closed from the error page. The steps are set in place, so I'm looking for a solution for the current architecture.
On the error page, I've tried calling a function from the same script file that includes the function responsible for opening the popup window. The related code is:
var PopupWnd = null;
function ShowPopup(){
PopupWnd = window.open('Popup.aspx','PopupWnd','');
}
function HidePopup(){
if(PopupWnd != null){
if(!PopupWnd.closed){
PopupWnd.close();
}
}
}
From the HidePopup() function, shouldn't the PopupWnd object be not equal to null if the ShowPopup() function was executed earlier? Is it related to a scoping issue or timing of events? How do I make the PopupWnd object value persist so that it's not equal to null when I want to close the popup window in the HidePopup() function?
ADDITIONAL NOTES:
After confirming that there isn't a collision in names, I'm wondering if it matters that I'm calling the HidePopup function from the Page_Load event of my code behind of the error aspx page. The code is:
string script = "<script>HidePopup();</script>";
ClientScript.RegisterClientScriptBlock(GetType(), "UploadErrorScript", script);
For a sanity check, I entered in alerts to make sure that the HidePopup function is being called from my codebehind and the PopupWnd object is still set equal to null within this function.

You may have a collision in the names. You have a variable PopupWnd and the name of the window PopupWnd.
var PopupWnd = null; function
ShowPopup(){
PopupWnd = window.open('Popup.aspx','PopupWindow','');
}
function HidePopup(){
if(PopupWnd != null){
if(!PopupWnd.closed){
PopupWnd.close();
}
}
}
That should work as the WindowName no longer collides with the variable. I'd suggest checking out http://www.infimum.dk/HTML/JSwindows.html for a more indepth view on window manipulation using javascript.
In a case like this, I'd also suggest Line by Line testing and doing things such as throwing out an alert i.e. do an alert after if PopupWnd != Null to see if it even reaches that part. Hell, do an alert right at th ebeginning of the HidePopup function to see what the value of PopupWnd is...

Related

How do I navigate to bing.com and enter a search text using the chrome console?

Below is my code.
It is resulting in unexpected behaviour.
It navigates to bing.com but it does not fill in the text field. Also, I have noticed that the console get cleared after navigating to a new webpage.
window.location = "https://www.bing.com";
window.onload = function(){
var editSearch = document.getElementById("sb_form_q");
editSearch.value = "Quux";
}
You are binding the onload function to the existing window object.
When the browser loads the new page, it will make a new window object which won't have your property set on it.
JavaScript run in one page (even when you are running it through developer tools) can't persist variables onto a different page.
(Storage mechanisms like localStorage and cookies are available, but you would need code in the subsequent page to look for them).
JavaScript is only valid for the current page you are on. When you are executing code from DevTools console, you are executing code on that page itself. So, when you navigate to another page using window.location you loose the onload handler you have defined.
To add handlers to a different page, it must be connected to your page (the parent) in some way, like an iframe or a popup.
ifrm = document.getElementById('frame');
ifrm.src = 'http://example.com';
ifrm.contentWindow.onload = function () {
// do something here with
// ifrm.contentWindow.document.getElementById('form')
}
As #Quentin said.
But you can do another way like ..
var keyword = "Quux";
window.location = "https://www.bing.com/search?q="+keyword;

Javascript: How to make sure window.open returns same window, if already opened

I am working on a web based application, in which I have to open popup window. I am using window.open() method to open the popup, like this:
window.open(url, "popupWin");
where url contains the URL I would like my popup window to navigate to. Now, the problem is, if I execute window.open() from multiple tabs (with same or different URLs), at least on Chrome, it might / might not give you the same window which was opened earlier. This behaviour is inconsistent, I mean, either it should get me fresh window every time, or it should get me previously opened window every time.
I need to persist the same popup window for entire domain. How can I do that?
Well looks like there is a direction to go or at least to give it a try.
It fully remains on localStorage which gives you ability to share the knowledge across your tabs within a single domain.
The code I give below does not work yet (it is only a direction), so don't expect too much from running it as it is.
What it does: it saves the popups by the url in a localStorage and when you try to open a new one with the same url it won't do that. If you don't want to distinguish them by URL it is even simpler: store boolean in a localStorage instead of an object.
What it does not do but should:
it should listen to the popup onunload (close) event and reset the localStorage information accordingly. Best for you here is just to set your localStorage boolean value to false
it should listen to the current tab onunload (reload, close) event and also reset something according to Your logic. As I understand the best for you would be just check whether this tab is the last one from your domain (you can also do this using localStorage, e.g. on every new tab adding its identifier, e.g. creation timestamp and destroying it on tab close) and if it is set your localStorage boolean value to false.
This, I think, would be enough to solve the problem. And finally a small piece of code:
// get the localstorage url map
function getOpenPopups() {
var obj = localStorage.getItem('mypopups');
return obj ? JSON.parse(obj) : {};
}
// set the localstorage url map
function setOpenPopups(object) {
localStorage.setItem('mypopups', JSON.stringify(object))
}
// open the popup
function popup(url, title) {
var popups = getOpenPopups();
// check whether popup with this url is already open
// if not then set it and open the popup
if (!popups[url]) {
popups[url] = true;
setOpenPopups(popups);
return window.open('abc', 'cde');
}
else {
return false;
}
}
jsFiddle
From w3c documentation we can see that window.open() returns a reference to the newly created window, or null if the call failed. That means we can keep it in memory and check for closed flag of that window.
var newWindow = window.open('/some/path', 'TestWindow');
// ...
if (!newWindow.closed) {
}
Keep in mind that if window with following name exists, page will be loaded in the same window without opening new one.
Other variants of name parameter like _blank, _self, _top, _parent you can find in official docs too.

Close Window Javascript

Is there a way i can create a javascript which will run the command onClick="my_onclick()" if they close the browser window? If so can you also make a popup that warns them If they close the window it will log out on the same script?
I am trying some variations of what i found but its not working for me.
$(document).ready(function()
{
$(window).bind("beforeunload", function() {
return confirm("If you confirm, you will be logged off the internet.");
});
});
I need it to return these paramters.
function my_onclick()
{URL = "http://%SERVERIP%/logout?%PARAMS%"; window.location.href=URL;}
The only 'event' available within Javascript that relates to the closing of a window is onbeforeunload, which expects to return a boolean (from the result of calling window.confirm().
It cannot be used to pass data back, since the page will already have expired. In short, no you can't do that.
You can use window.onbeforeunload, here's an example of what you need:
window.onbeforeunload = myConfirm;
function myConfirm(){
confirmMessage="If you confirm, you will be logged off !!";
return confirmMessage;
}

Is there any way to get the origin of an alert box?

I work with some very large and confusing JavaScript files that I did not write. Sometimes an alert will come up but I don't know where it's coming from.
You could search all files for the text contained in the alert but if that text is dynamic it won't work.
Is there a way to set a breakpoint in order to intercept an alert?
At the very top of your HTML:
window.alert = function() {
debugger;
}
debugger is a statement that invokes any debugging functionality available. With developer tools open, you'll automatically hit a breakpoint whenever alert is called. You can then inspect the call stack to see exactly what called the custom alert function.
It may or may not be helpful to you, but you can overwrite the alert function to do whatever you want with it. For example, instead of alert boxes, you could have it log the message to the console.
window.alert = function(msg) {
console.log(msg);
}
alert('test');
I agree with Brian Glaz, but in order to get more details (line number) you might try to throw an error when alerting something and outputting the error on the console. this way, the console will point you to the right line number where the alert function was called.
Put this snippet at the top of your document and give it a try :
var originalAlert = window.alert;
window.alert = function(){
try{
throw new Error('alert was called');
} catch(e){
console.warn(e);
}
return originalAlert.apply(window, arguments);
}
Open Chrome push F12 key and go to Sources.
Then choose a script file Ctrl+F and search for alert.
You can put breakpoint on any line you wish

Window.open doesn't return the window reference IE9

I'm using a script to mount my mailto link and call the default email editor. But I can't use document.location.href because of some bug in IE9, so I use window.open. It works. But I need to close the IE windows opened.
The problem is the window.close doesn't return the window reference.
function doMailto() {
var sMailto = 'mailto:?bcc=';
sMailto += document.getElementById('<%= txtEmails.ClientID %>').value;
out = window.open(sMailto);
out.close(); //CANT CALL CLOSE, BECAUSE OUT IS NULL
}
You have an extra paren at the end of the assignment line.
Also, are you trying to close the window in the same function as assigned? If not, you may need to declare the window variable outside the function so it can be closed when needed.
aside the syntax errors (two commas, two parentesis closing)...
you are opening a window that is external to the browser, your default mail client. You cannot control it through javascript.
Maybe it's an immediacy problem, try using:
var out = window.open()...
setTimeout(function(){out.close()}, 200)
and fiddle with the 200ms to see if it works then.
try
top.location.href = 'mailto:....';
you won't need to open or close any windows this way

Categories

Resources