Close Window in IE from javascript. (Small Hack)

When trying to close an window from javascript in IE, which is opened manually,
a security warning “The web page you are viewing is trying to close the window. Do you want to close this window? Yes|No ” is popping up. Is there any possibility to close a window silently? Yes.

A simple hack i found on the web will help to get rid of this situation.


this.focus();
self.opener = this;
self.close();

but in Firefox you can do this. Firefox will not allow you to close using the above code. Maybe anybody found/having any code post it here.

How to find your Popup is blocked by Popup blocker in javascript

How to find your Popup is blocked by Popup blocker in javascript when using window.open.

We open a window using window.open() in our pages using javascript. But if there is popup blocker sitting in the client machine and the user is not aware the popup blocker is blocking the popup window from your site, we can inform him the popup is blocked and ask him to remove that. How?


winHandle=window.open('yourfile.htm' + "?newwin=true", "_top", "width="+wwidth+",height="+wheight+",location=0,menubar=0,resizable=no,scrollbars=no,status=yes,titlebar=no,dependent=yes");
if(winHandle==null){
alert("Error: While Launching New Window...nYour browser maybe blocking up Popup windows. nn  Please check your Popup Blocker Settings");
}

How to Change an image’s width dynamically from Javascript

How to Change an image’s width dynamically from Javascript. Using DOM we can do this.

function changeImgSize(objectId,newWidth,newHeight) {
imgString = 'theImg = document.getElementById("'+objectId+'")';
eval(imgString);
oldWidth = theImg.width;
oldHeight = theImg.height;
if(newWidth>0){
theImg.width = newWidth;
}
if(newHeight>0){
theImg.height = newHeight;
}
}

Note: this one i found on the web but i can’t remember where i found. Credits to the author.

Check whether an HTML element/Object exists or not.

This small javascript function will help you to check if a HTML object.Element exists in a page from the page itself or from a popup window.


function chkObject(inParent,theVal) {
if(inParent){
if (window.opener.document.getElementById(theVal) != null) {
return true;
} else {
return false;
}
}else{
if (document.getElementById(theVal) != null) {
return true;
} else {
return false;
}
}
}

Note:
chks whether an object exists or not in the document.
if the inParent is true then the object/elt is checked for its existence in the parent/opener’s document.
i.e., it will be useful to chk from a popup window.
To check in the current document itself , then set inParent value as false.

To load an doc inside a iframe dynamically

We can load a document into an iframe dynamically by using iframe.src = “yourpage” in javascript.
But this works well in IE and not in others browsers. Here is an method to load iframes dynamically.

I found this at http://developer.apple.com/internet/webcontent/iframe.html and just changed something to fit our need.


var IFrameObj;
function loadIFrame(thePage){
//loads an doc inside a iframe dynamically
URL = thePage;
IFrameObj = new Object();
IFrameObj.document = new Object();
IFrameObj.document.location = new Object();
IFrameObj.document.location.iframe = document.getElementById('cpaneif');
IFrameObj.document.location.replace = function(location) {
this.iframe.src = location;
}

if (navigator.userAgent.indexOf(‘Gecko’) !=-1 && !IFrameObj.contentDocument) {
// allow a fraction of a second to recognize the new IFrame
//setTimeout(‘loadIFrame(“‘+thePage+'”)’,10);
//return false;
}

if (IFrameObj.contentDocument) {
// For NS6
IFrameDoc = IFrameObj.contentDocument;
} else if (IFrameObj.contentWindow) {
// For IE5.5 and IE6
IFrameDoc = IFrameObj.contentWindow.document;
} else if (IFrameObj.document) {
// For IE5
IFrameDoc = IFrameObj.document;
} else {
return true;
}

IFrameDoc.location.replace(URL);
/* NB:-
//if you want to create a new iframe dynamically visit
http://developer.apple.com/internet/webcontent/iframe.html
*/
}