Page 1 of 1

Error when using .insert()?

PostPosted: Thu Oct 23, 2008 3:38 pm
by Lashiec
I get this error:

uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMNSHTMLDocument.execCommand]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: http://127.0.0.1/Classes/Email/scripts/ ... or.pack.js :: anonymous :: line 1" data: no]

What I'm doing is this:

I have a drop down box, so when someone selects an item from the list it inputs a message in the WYM box (grabs from a database). It works fine, but if I type anything in the WYM box and try the drop down menu, it does that.

This is my code... It also seems to add an extra <p></p> in the display window of the WYM box, but it doesn't actually show up in the HTML for the WYM box. I also think the line that shows wym.html(''); could be wrong. I wanted to clear the box before it does the .insert(data)

Code: Select all
$(document).ready(function() {
         jQuery(function() {
            jQuery('.wymeditor').wymeditor({
               postInit: function(wym){
                 $('#template_box').bind('change', function(e){
                     $num = $('#template_box').val();
                     $.post('admin/ajax.php?action=get_template', {id: $num},
                        function(data) {
                           wym.html('');
                           wym.insert(data);
                        })
                  })
               }
            });
         });
      })

Re: Error when using .insert()?

PostPosted: Tue Oct 28, 2008 5:04 pm
by mr_lundis
Hello!

Do you think that you could set up some kind of test page for us? It would make helping you it a lot easier!

Cheers! :wink:

Re: Error when using .insert()?

PostPosted: Sun Nov 02, 2008 12:04 am
by Lashiec
Yeah, no problem. When I get back to work on Monday I'll set the page up on our server and post a link here. Thanks!

Re: Error when using .insert()?

PostPosted: Mon Nov 03, 2008 2:09 pm
by Lashiec
http://24.230.153.90/Email/template.php

That's the page that throws the error. If you pick an item from the drop down menu it puts text in the wymeditor, it also inserts an extra <p> line, which doesn't appear in the HTML. Also, if you type in the wymeditor box and then switch the drop down menu it throws out that error.

It's doing an ajax call to another page that outputs the html when you switch the drop down menu. It then takes the returned html and inserts it.

Re: Error when using .insert()?

PostPosted: Mon Nov 03, 2008 7:09 pm
by mr_lundis
Okay, now I've taken a look at you page...

The error you get is because you try to insert code into a selection that doesn't exist (as the editor has been emptied), this is also why you get that extra paragraph, as the insert method falls back to the paste method when there's no selection. However, when you click inside the editor, you crate a new selection, but the html elements that is refers to gets erased when you call the html method, thus the error, as the selection still exist when insert is called.

If you do it like this instead you will go around all these problems, it's also a more efficient way of doing things.
Code: Select all
$(document).ready(function() {
         jQuery(function() {
            jQuery('.wymeditor').wymeditor({
               postInit: function(wym){
                 $('#template_box').bind('change', function(e){
                     $num = $('#template_box').val();
                     $.post('admin/ajax.php?action=get_template', {id: $num},
                        function(data) {
                           wym.html(data);
                        })
                  })
               }
            });
         });
      })


As you see, the html method takes any string as an argument, and it will replace the editor content with the string provided.

Cheers!