IE/Mozilla WYSIWYG editor - problems with IE and queryCommandEnabled

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Justin Koivisto

    IE/Mozilla WYSIWYG editor - problems with IE and queryCommandEnabled

    I am having a problem with Internet Explorer and javascript on a WYSIWYG
    editor I am creating. It all works in Mozilla, but IE returns flase for
    queryCommandEna bled('forecol') . JS isn't my strong suit, so I am turning
    to all you experts for help. What am I missing here? Other commands like
    formatblock, fontsize and fontname all work fine, so I assume that it
    has something to do with not getting the correct selection because of
    the iframe. Any help is greatly appreciated.

    In an HTML document I have the following line:

    <img src="images/forecol.png" onClick="DoText Format('forecol or','')"
    id="forecolor" >
    <iframe width="175" height="125" id="forecolor0 " src="palette.ht ml"
    style="visibili ty: hidden; position: absolute; left: 0px; top:
    0px;"></iframe>



    palette.html is like this:
    <html>
    <head>
    <title>Color Palete</title>
    <script language="JavaS cript" type="text/javascript">
    <!--
    function selectColor(col or){
    self.parent.set Color(color);
    }
    function InitColorPalett e(){
    if (document.getEl ementsByTagName )
    var x = document.getEle mentsByTagName( 'TD');
    else if (document.all)
    var x = document.all.ta gs('TD');
    for (var i=0;i<x.length; i++){
    x[i].onclick = click;
    }
    }
    function click(){
    str=this.id;
    color=str.repla ce('c','#');
    selectColor(col or);
    }
    -->
    </script>
    <style type="text/css">
    <!--
    html,body{margi n: 0px; padding: 0px;}
    td{border: black none 2px;}
    td:hover{border : white solid 2px;}
    -->
    </style>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    </head>

    <body bgcolor="white" onLoad="InitCol orPalette()">
    <table border="0" cellpadding="0" cellspacing="2" >
    <tr>
    <td id="cFFFFFF" bgcolor="#FFFFF F" width="15" height="15">&nb sp;</td>
    <td id="cFFCCCC" bgcolor="#FFCCC C" width="15" height="15">&nb sp;</td>
    ....



    In the included javascript source file I have the following functions:

    function DoTextFormat(co mmand, option){
    if((command=='f orecolor') || (command=='hili tecolor')){
    parent.command= command;
    layerid=command +'0';
    buttonElement=d ocument.getElem entById(command );

    document.getEle mentById(layeri d).style.left=g etOffsetLeft(bu ttonElement)
    + 'px';

    document.getEle mentById(layeri d).style.top=(g etOffsetTop(but tonElement)
    + buttonElement.o ffsetHeight) + 'px';
    if(document.get ElementById(lay erid).style.vis ibility=='hidde n')
    document.getEle mentById(layeri d).style.visibi lity='visible';
    else{
    document.getEle mentById(layeri d).style.visibi lity='hidden';
    }

    //get current selected range
    var
    sel=document.ge tElementById('i View').contentW indow.document. selection;
    if(sel!=null){
    rng=sel.createR ange();
    }
    }else{

    if(document.get ElementById('iV iew').contentWi ndow.document.q ueryCommandEnab led(command)){

    document.getEle mentById('iView ').contentWindo w.document.exec Command(command ,
    false, option);
    return true;
    }else return false;
    }
    document.getEle mentById('iView ').contentWindo w.focus();
    }

    function setColor(color) {
    elem=command+'0 ';
    if(browser.isIE 5up){
    //retrieve selected range
    var
    sel=document.ge tElementById('i View').contentW indow.document. selection;
    if(sel!=null){
    var newselectionRan ge=sel.createRa nge();
    newselectionRan ge.select();
    }
    }else{
    document.getEle mentById('iView ').contentWindo w.focus();
    }

    if(document.get ElementById('iV iew').contentWi ndow.document.q ueryCommandEnab led(command)){

    document.getEle mentById('iView ').contentWindo w.document.exec Command(command ,
    false, color);
    }else{

    alert("queryCom mandEnabled("+c ommand+") returned false. Cannot set color
    to "+color);
    // close palette before exiting function
    document.getEle mentById(elem). style.visibilit y='hidden';
    return false;
    }
    document.getEle mentById('iView ').contentWindo w.focus();
    document.getEle mentById(elem). style.visibilit y='hidden';
    return true;
    }

    TIA

    --
    Justin Koivisto - spam@koivi.com
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

  • Nobody

    #2
    Re: IE/Mozilla WYSIWYG editor - problems with IE and queryCommandEna bled

    I'm not sure what your exact problem is, but you might wish to incorporate
    some of this type of logic to prevent errors on obscure browser versions and
    variations, Macs and the like...: Good client-side script should never
    reference a browser by name.

    <html>
    <head>
    <script language="javas cript">
    function test() {
    if (frames.length && frames['iView'] && frames['iView'].document &&
    frames['iView'].document.query CommandEnabled) {
    alert(frames['iView'].document.query CommandEnabled( "forecolor" ))
    }
    else
    if (document.getEl ementById &&
    document.getEle mentById('iView ').contentWindo w &&
    document.getEle mentById('iView ').contentWindo w.queryCommandE nabled)

    alert(document. getElementById( 'iView').conten tWindow.documen t.queryCommandE n
    abled("forecolo r"))
    else
    alert("Browser cannot handle scripted editing!")
    }
    onload = test;
    </script>
    </head>
    <body>
    <iframe id="iView" name="iView" src="test.html" ></iframe>
    </body>
    </html>




    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: IE/Mozilla WYSIWYG editor - problems with IE and queryCommandEna bled

      "Nobody" <none@nope.ne t> writes:
      [color=blue]
      > Good client-side script should never reference a browser by name.[/color]

      I'll have to disagree on that.

      While I advocate writing to the standards, some pages also have to
      support browsers where the standards based methods won't work
      (typically NS4 or IE4).

      For such pages, I prefer detecting the browser specifically and make
      exceptions for it. There is no absolutely safe method of detecting the
      browser (some of the lie), but checking for the name is better than
      many other checks.

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Nobody

        #4
        Re: IE/Mozilla WYSIWYG editor - problems with IE and queryCommandEna bled


        "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
        news:k78p4zqg.f sf@hotpop.com.. .
        | "Nobody" <none@nope.ne t> writes:
        |
        | > Good client-side script should never reference a browser by name.
        |
        | I'll have to disagree on that.
        |
        | While I advocate writing to the standards, some pages also have to
        | support browsers where the standards based methods won't work
        | (typically NS4 or IE4).

        NS4 is very easy. document.layers

        IE$ is document.all && !document.getEl ementByID (sp?)

        IE4 typically needs no extra coding. NS4 should be supported by very
        low-level scripts (typically wrappers for finding and positioning elements.)

        |
        | For such pages, I prefer detecting the browser specifically and make
        | exceptions for it. There is no absolutely safe method of detecting the
        | browser (some of the lie), but checking for the name is better than
        | many other checks.
        |
        | /L
        | --
        | Lasse Reichstein Nielsen - lrn@hotpop.com
        | Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        | 'Faith without judgement merely degrades the spirit divine.'


        Comment

        • Justin Koivisto

          #5
          Re: IE/Mozilla WYSIWYG editor - problems with IE and queryCommandEna bled

          Justin Koivisto wrote:[color=blue]
          > I am having a problem with Internet Explorer and javascript on a WYSIWYG
          > editor I am creating. It all works in Mozilla, but IE returns flase for
          > queryCommandEna bled('forecol') . JS isn't my strong suit, so I am turning
          > to all you experts for help. What am I missing here? Other commands like
          > formatblock, fontsize and fontname all work fine, so I assume that it
          > has something to do with not getting the correct selection because of
          > the iframe. Any help is greatly appreciated.[/color]

          Hopefully this will help get some input on the problem - here is a URL
          to view it all:


          As far as I can tell, this is the last piece of the puzzle for me to get
          it into the CMS that I am developing. Everything works fine in Mozilla
          (and what I've tested thus far in NS6+), but the color issue isn't
          working with IE. Everything for this project was going quite smoothly
          for not doing JS before, but this part is really getting to me (I've let
          it sit for a couple months and am just getting back to it now).

          Also, any pointers on how to create tables in IE as done in Mozilla
          would be quite helpful as well. TIA!

          --
          Justin Koivisto - spam@koivi.com
          PHP POSTERS: Please use comp.lang.php for PHP related questions,
          alt.php* groups are not recommended.

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: IE/Mozilla WYSIWYG editor - problems with IE and queryCommandEna bled

            "Nobody" <none@nope.ne t> writes:
            [color=blue]
            > NS4 is very easy. document.layers[/color]

            OmniWeb.
            <URL:http://www.omnigroup.c om/mailman/archive/omniweb-l/2002-June/007067.html>
            [color=blue]
            > IE$ is document.all && !document.getEl ementByID (sp?)[/color]

            That might be unique (since hopefully no new browser will ever be made
            without getElementById) .
            [color=blue]
            > IE4 typically needs no extra coding. NS4 should be supported by very
            > low-level scripts (typically wrappers for finding and positioning elements.)[/color]

            IE 4 lacks a few features that I use often in Javascript (e.g.,
            functional arguments to setTimeout/setInterval and to String.replace)
            as well as a standards compliant rendering mode.
            Ofcourse, the Javascript part can be fixed it with wrappers too.

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Nobody

              #7
              Re: IE/Mozilla WYSIWYG editor - problems with IE and queryCommandEna bled


              "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
              news:n0dl394f.f sf@hotpop.com.. .
              | "Nobody" <none@nope.ne t> writes:
              |
              | > NS4 is very easy. document.layers
              |
              | OmniWeb.
              |
              <URL:http://www.omnigroup.com/mailman/arc...une/007067.htm
              l>

              Thanks. What are those jerks doing? What is the more specific solution to
              weed them out of the equation? Create a test layer and see if it actually
              has properties?

              |
              | > IE$ is document.all && !document.getEl ementByID (sp?)
              |
              | That might be unique (since hopefully no new browser will ever be made
              | without getElementById) .

              Lets hope!

              |
              | > IE4 typically needs no extra coding. NS4 should be supported by very
              | > low-level scripts (typically wrappers for finding and positioning
              elements.)
              |
              | IE 4 lacks a few features that I use often in Javascript (e.g.,
              | functional arguments to setTimeout/setInterval and to String.replace)
              | as well as a standards compliant rendering mode.
              | Ofcourse, the Javascript part can be fixed it with wrappers too.

              Function arguments to setTimeout? Can you elaborate on that one?
              String.replace would need a wrapper if you use that.

              |
              | /L
              | --
              | Lasse Reichstein Nielsen - lrn@hotpop.com
              | Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
              | 'Faith without judgement merely degrades the spirit divine.'


              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: IE/Mozilla WYSIWYG editor - problems with IE and queryCommandEna bled

                "Nobody" <none@nope.ne t> writes:
                [color=blue]
                > Thanks. What are those jerks doing? What is the more specific solution to
                > weed them out of the equation? Create a test layer and see if it actually
                > has properties?[/color]

                I would just check for document.getEle mentById and document.all first
                (in that order). If they support either, they will be fine. If not,
                then I'll have to rely on document.layers working, and if it doesn't,
                it is a doomed browser anyway.
                [color=blue]
                > Function arguments to setTimeout? Can you elaborate on that one?[/color]

                You can use setTimeout in two ways: with a string or a function.
                setTimeout("foo =bar;",2000);
                or
                setTimeout(func tion(){foo=bar; },2000);

                The latter method has several advantages:
                - It detects syntax errors early (during parsing, not when the code is run
                for the first time);
                - I find it easier to separate code and data when the code isn't inside
                a string.
                - The closure keeps the references to local variables (*much* easier coding!),
                while the string is evaluated in the global scope.

                Sadly, IE4 only supports the first. The same goes for setInterval.

                According to MSDN
                (<URL:http://msdn.microsoft. com/library/default.asp?url =/workshop/author/dhtml/dhtml_node_entr y.asp>)
                ---
                In versions earlier than Microsoft® Internet Explorer 5, the first
                argument of setTimeout must be a string. Evaluation of the string is
                deferred until the specified interval elapses.
                ---

                [color=blue]
                > String.replace would need a wrapper if you use that.[/color]

                It doesn't work in IE5.0 either, only in 5.5 and 6.0.

                Hmm, in Opera 7, String.prototyp e.replace is a Javascript function,
                not an internal function. I could probably just use that code if I
                need it :)

                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • Nobody

                  #9
                  Re: IE/Mozilla WYSIWYG editor - problems with IE and queryCommandEna bled


                  "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
                  news:vfs91p8a.f sf@hotpop.com.. .
                  | "Nobody" <none@nope.ne t> writes:
                  |
                  | > Thanks. What are those jerks doing? What is the more specific solution
                  to
                  | > weed them out of the equation? Create a test layer and see if it
                  actually
                  | > has properties?
                  |
                  | I would just check for document.getEle mentById and document.all first
                  | (in that order). If they support either, they will be fine. If not,
                  | then I'll have to rely on document.layers working, and if it doesn't,
                  | it is a doomed browser anyway.

                  Makes sense. Doomed browsers indeed.

                  |
                  | > Function arguments to setTimeout? Can you elaborate on that one?
                  |
                  | You can use setTimeout in two ways: with a string or a function.
                  | setTimeout("foo =bar;",2000);
                  | or
                  | setTimeout(func tion(){foo=bar; },2000);
                  |
                  | The latter method has several advantages:
                  | - It detects syntax errors early (during parsing, not when the code is run
                  | for the first time);
                  | - I find it easier to separate code and data when the code isn't inside
                  | a string.
                  | - The closure keeps the references to local variables (*much* easier
                  coding!),

                  Bingo was his name-o! That's the one that matters (to me at least.) I'll
                  keep that in mind for future, but thank goodness that I am not using it
                  anywhere at present (because I know I don't check for IE4 before setting
                  timeouts!)

                  | while the string is evaluated in the global scope.
                  |
                  | Sadly, IE4 only supports the first. The same goes for setInterval.

                  Don't use the latter and can't even remember what it does.

                  |
                  | According to MSDN
                  |
                  (<URL:http://msdn.microsoft.com/library/de...hop/author/dht
                  ml/dhtml_node_entr y.asp>)
                  | ---
                  | In versions earlier than Microsoft® Internet Explorer 5, the first
                  | argument of setTimeout must be a string. Evaluation of the string is
                  | deferred until the specified interval elapses.
                  | ---
                  |
                  |
                  | > String.replace would need a wrapper if you use that.
                  |
                  | It doesn't work in IE5.0 either, only in 5.5 and 6.0.

                  Noted. Can't you check for the existence of the replace method on a string
                  object (so as to forget the browser versions.)

                  |
                  | Hmm, in Opera 7, String.prototyp e.replace is a Javascript function,
                  | not an internal function. I could probably just use that code if I
                  | need it :)

                  I don't use it on the client side (though I am sure that some of my code
                  would be made more efficient with it.)

                  Thanks for your input!

                  |
                  | /L
                  | --
                  | Lasse Reichstein Nielsen - lrn@hotpop.com
                  | Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                  | 'Faith without judgement merely degrades the spirit divine.'


                  Comment

                  • Jim Ley

                    #10
                    Re: IE/Mozilla WYSIWYG editor - problems with IE and queryCommandEna bled

                    On 04 Sep 2003 00:28:21 +0200, Lasse Reichstein Nielsen
                    <lrn@hotpop.com > wrote:
                    [color=blue][color=green]
                    >> String.replace would need a wrapper if you use that.[/color]
                    >
                    >It doesn't work in IE5.0 either, only in 5.5 and 6.0.[/color]

                    That could even fail in IE6 since it's a JS method not a DOM method,
                    and js version is strictly independant from browser version....

                    Jim.
                    --
                    comp.lang.javas cript FAQ - http://jibbering.com/faq/

                    Comment

                    • Lasse Reichstein Nielsen

                      #11
                      Re: IE/Mozilla WYSIWYG editor - problems with IE and queryCommandEna bled

                      "Nobody" <none@nope.ne t> writes:
                      [color=blue]
                      > Noted. Can't you check for the existence of the replace method on a string
                      > object (so as to forget the browser versions.)[/color]

                      The replace function is there, it's just that the replaceText argument
                      can't be a function in IE 5.0 or earlier. It works in Netscape 4.

                      Example of replacing with a function:
                      ---
                      function reverseString(s tr) {
                      return str.split("").r everse().join(" ");
                      }
                      var text = "this becomes a weird text";
                      var newText = text.replace(/\b\w*\b/g,reverseString );
                      alert(newText);
                      ---

                      If using a replaceText string, you can also use, e.g., $1 to refer to
                      the first matched group. This only works in IE5.5+ too. Before that,
                      the replace method could only replace with a fixed string.

                      /L
                      --
                      Lasse Reichstein Nielsen - lrn@hotpop.com
                      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                      'Faith without judgement merely degrades the spirit divine.'

                      Comment

                      • Lasse Reichstein Nielsen

                        #12
                        Re: IE/Mozilla WYSIWYG editor - problems with IE and queryCommandEna bled

                        jim@jibbering.c om (Jim Ley) writes:
                        [color=blue]
                        > That could even fail in IE6 since it's a JS method not a DOM method,
                        > and js version is strictly independant from browser version....[/color]

                        That is a point, but I believe IE 6 includes JScript 5.6. Perhaps you
                        could install an earler version of JScript, but I am not sure.

                        <URL:http://msdn.microsoft. com/library/default.asp?url =/library/en-us/script56/html/js56jsoriversio ninformation.as p>

                        /L
                        --
                        Lasse Reichstein Nielsen - lrn@hotpop.com
                        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                        'Faith without judgement merely degrades the spirit divine.'

                        Comment

                        • Jim Ley

                          #13
                          Re: IE/Mozilla WYSIWYG editor - problems with IE and queryCommandEna bled

                          On 04 Sep 2003 01:27:32 +0200, Lasse Reichstein Nielsen
                          <lrn@hotpop.com > wrote:
                          [color=blue]
                          >jim@jibbering. com (Jim Ley) writes:
                          >[color=green]
                          >> That could even fail in IE6 since it's a JS method not a DOM method,
                          >> and js version is strictly independant from browser version....[/color]
                          >
                          >That is a point, but I believe IE 6 includes JScript 5.6.[/color]

                          Oh in reality the chances are very slim, some sort of install problem
                          whereby jscript.dll couldn't be written is about the only option,
                          that's not an intentional attempt, not likely ay all, that's what I
                          was intending to get across with the "strictly" did a very bad job
                          though. Sorry.

                          Hun.
                          --
                          comp.lang.javas cript FAQ - http://jibbering.com/faq/

                          Comment

                          Working...