Here's the picture:
I need to convert an old application, and avoid it to open popup, but place DHTML divs over the main page instead.
As I'm a bit lazy (and always up for a brainf*ck), i'd prefer to leave the code in those popups untouched, and I was wondering if I could convert them without altering them.
Now, I probably will go through modifiying the 200+ windows, but this have started to go through my mind for the last hours...
I analyzed it should go through those steps:
1) Finding if a click have hapenned over a link (I'm ingnoring the onclick for now... I already want to get the base working)
2) Was the destination of this link a window.open ?
3) If so, stop the event, and open the popup in a custom made div+iframe
4) When the iframe have a form who wants to pass a value to the opener, catch it
5) When the iframe issue a window.close() statement, dismiss the div+iframe
I started by writing a JS class that will handle all of that.That's nearly done.
My problem now, is that to catch a window.close() call from the inside of the iframe, I need to overload the window object close function.
Don't even try to run it on IE for now, I'm 429% sure it won't run...
That little fscker...
I tried to do:
Code:
window.frames['id_if_iframe'].close=function(){alert('closing now!'};
or
Code:
window.frames['id_if_iframe'].prototype={close:function(){alert('closing now!'}};
FF accept it without a hitch, but when I try to call them, I still have the old behavior.
So, as far as I know, we can extend native JS objects via the prototype, but can we alter existing functions ?
I tried it, I've searched it, but I didn't found any resource that could confirm it.
Does any of you know ?
HTML Code:
<html>
<head>
<title>Grab tester</title>
<script type="text/javascript" src="grab.js"></script>
<script type="text/javascript">
function open1(){
window.open('http://www.google.com','gglWin','');
}
</script>
<style type="text/css">
.popup{
position:fixed;
left:20p";
boder:2px solid black;
display:block;
background-Color:gray;
}
</style>
</head>
<body>
<a href="javascript:open1();">Google 1</a><br>
<a id="aja" href="javascript:window.open('http://www.google.com','gglWin','');">Google 2</a><br>
<a href="void.html">null</a>
</body>
</html>
Code:
clCatcher= function(){
var cnt=0;
var popupId=null;
}
clCatcher.prototype={
init: function(){
this.setHandler();
}
,
setHandler: function(){
try{
window.addEventListener('click', catcher.grabOpen, true);
}
catch(err){
document.attachEvent('onclick', catcher.grabOpen);
}
}
,
openerChk: function(elm){
var ret=false;
if(elm.href){
if(elm.href.indexOf('window.open')!=-1){
ret=true;
}
else if(elm.onclick.indexOf('window.open')!=-1){
ret=true;
}
else{
ret=false;
}
}
return ret;
}
,
getPopUrl: function(elm){
if(elm.href.indexOf('window.open')!=-1){
var opLine=elm.href;
}
else{
var opLine=elm.onclick;
}
var ary=opLine.split(",");
var url=ary[0];
url=url.replace('javascript:','');
url=url.replace('window.open(','');
url=url.replace(');','');
url=url.replace("'","");
url=url.replace(String.fromCharCode(39),"");
return url;
}
,
grabOpen: function(e){
var trg=e.target;
if(catcher.openerChk(trg)==true){
e.preventDefault();
e.stopPropagation();
var hr=catcher.getPopUrl(trg);
catcher.setupDiv(hr);
}
}
,
dismissDiv: function(){
var ary=document.getElementsByTagName('div');
for(var i=0;i<ary.length;i++){
if(ary[i].id==catcher.popupId){
ary[i].parentNode.removeChild(ary[i]);
}
}
}
,
setupDiv: function(url){
var ndiv=document.createElement('div');
var rdm="popDiv_"+parseInt(Math.random()*10000000);
ndiv.id=rdm;
catcher.popupId=rdm;
ndiv.className="popup"
ndiv.style.top=parseInt(window.pageXOffset+20)+"px";
ndiv.style.width=parseInt(window.innerWidth-20)+"px";
ndiv.style.height=parseInt(window.innerHeight-60)+"px";
var iframe=document.createElement('iframe');
iframe.style.width="100%";
iframe.style.height="100%";
iframe.src=url;
iframe.id="if_"+rdm;
iframe.name=iframe.id;
var btn=document.createElement('input');
btn.type="button";
btn.value="Close popup";
try{
btn.addEventListener('click', catcher.dismissDiv, true);
}
catch(err){
btn.attachEvent('onclick',catcher.dismissDiv);
}
ndiv.appendChild(iframe);
ndiv.appendChild(btn);
document.getElementsByTagName('body')[0].appendChild(ndiv);
eval('window.frames[\''+iframe.name+'\'].close=function(){parent.catcher.dismissDiv('+rdm+')}');
}
}
function describe(str){
var txt="";
for(var i=0;i<str.length;i++){
txt+=str[i]+":"+str.charCodeAt(i)+"\n";
}
alert(txt);
}
catcher=new clCatcher();
catcher.init();