Re: hrefs with javascript calls seem to interfere with Cute Editor

  •  04-20-2006, 4:16 PM

    Re: hrefs with javascript calls seem to interfere with Cute Editor

    Well, I hacked out a solution.  This is almost certainly overkill, so I'd appreciate it if anyone has a better solution.  In our case, we have links like ...href=jav ascr ipt: doSomething() in the page.  These seem to break the Cute Editor if clicked before the Editor is used (see explanation in my previous post).  Worse, our pages spawn popup controls that are full of more such links. 

    My way out of it was to call the following scripts in the onload event of the body tag, e.g.
    ...body onload="hideScriptedHrefs(getAllElements());"

    This drags through the document.all (we tried getElementsByTagName("a") -- it didn't find everything) and replaces every jav ascr ipt href with an onclick that does the same thing.  These new onclicks then also call the same function a second time (this was how we dealt with the popup control links, which only appear after the first click).

    If no one else has a better solution, I hope at least that this helps someone.  (PLEASE NOTE: i've had to create a few syntax errors (e.g. spelling the language "jav ascr ipt") to prevent the code from getting too marked up during post.)

        //use as a generic way to get a collection of all document elements
        function getAllElements(){
            //firefox seems to muddle through document.all as of version 1.5
            return document.all;
        }
       
        //use to disable jav ascr ipt:[function] style hrefs, while still preserving their behavior
        //this prevents loss of jav ascr ipt scope, but still executes the href scr ipt
        //(loss of jav ascr ipt scope seems to damage the CuteEditor)
        function hideScriptedHrefs(arr){
            var re = new RegExp(("jav" + "ascr" + "ipt:"),"gi");
            for (var i = 0; i < arr.length; i++){
                var el = arr[ i ];
                if (el.getAttribute('href') != null && el.getAttribute('href').match(re)){
                    el.setAttribute('formerHref',el.getAttribute('href'));
                    el.setAttribute('formerOnClick',el.getAttribute('onclick'));
                    el.href = "#";
                    el.onclick = function(){
                                    if (this.getAttribute('formerOnClick') != null) this.getAttribute('formerOnClick').call(this);
                                    if (this.getAttribute('formerHref') != null) eval(unescape(this.getAttribute('formerHref').replace(re,'')));
                                    hideScriptedHrefs(getAllElements()); /*only needed if new links might appear */
                                    return false;
                                    };
                }
            }
        }
View Complete Thread