Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Closed Thread
Overriding native javascript functions, is it possible ?
Old 06-25-2007, 09:40 AM Overriding native javascript functions, is it possible ?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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();
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
View Public Profile Visit tripy's homepage!
 
 
Register now for full access!
Old 06-29-2007, 06:52 PM Re: Overriding native javascript functions, is it possible ?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
My goodness,

That was a real brainer, but I did it !
should be totally compatible IE and firefox.

It should degrade nicely on unsupported browser, as if they cannot reassign the window.open() and window.close() function, the natives one should be used. But this is still untested.

I post the results here, as somebody might be interested.
The whole thing need a bit more work, as the "popup" are not movable now, and if multiples are opened, they pile up, with just a small offset, but they hide themselves.

What I tested is
window.open(url,name,opts)
If the options are passed, height and width are catched, and the iframe is on the specified width/height
Calling window.open() again, with the same name updates the current "popup" location of that specified name, like a real popup.

self.close() in the "popup", close it (duh...)

opener.formName.value in the popup reference the root document, so passing values to a form in the root document works, like it does in a popup.

window.close('window_name') closes the specified popup, if it's found in the currently displayed stack.

Last goodie, the original window.open() and window.close() functions are still reachables.
They are renamed window._open() and window._close()

I'll implement z-ordering, and drag&drop later, but I'm already fond of what I did.

See it in action there (in French, for now):
http://webalis.com/open/
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 06-29-2007 at 06:54 PM..
tripy is offline
View Public Profile Visit tripy's homepage!
 
Old 02-24-2009, 05:32 PM Re: Overriding native javascript functions, is it possible ?
Junior Talker

Posts: 2
Trades: 0
Hi, my company has a bunch of web pages on which they want to record every DOM event(mouse movement, clicks etc), these pages were written in past and now with minimal editing I need to add listener to capture these events.
Can you provide an easy and effective way to do this (may be addition of eventlistener at Body tag to capture all bubbled up events, but i am not sure if it will work when stopPropagation method is called by some eventlistener in the chain [specially facing problem with IE in overriding this]).
I am looking for a universal listener kind of thing that can be easily integrated with my pages.

Your work seemed very similar to what i am trying so can you please guide me in this.
bsnl1 is offline
View Public Profile
 
Closed Thread     « Reply to Overriding native javascript functions, is it possible ?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.18585 seconds with 12 queries