tags:

views:

445

answers:

5

Hello all,

I've been using a rteEditor very sucefully until now.

The problem is in this line of code:

document.getElementById(rteName).contentWindow.document.execCommand('insertHTML', false, html);

I'm passing an ABSOLUTE path to the html var such as ("http://www.url.com/file.html").

But when it execute this insert command the output is ("../file.html");

Its possible to use a jQuery command instead?

Any Suggestions?

+1  A: 

Have you tried using 'insertImage' instead of 'insertHTML'?

Edit: 'insertImage' just takes the url of the image and creates an img tag based on that.
You can get the image after inserting it with jQuery like this:

 var img = $("img[src='imgUrl']");

with 'imgUrl' being the url of the image you add, and then add the needed attributes to that.

An example without using jQuery is here at line 123.

rosscj2533
the problem now is that i need to pass other img parameters but the output fall in something like this: <img _moz_dirty="" src="http://colegas.fiberinteractive.com.br/imgconteudo/5cde7c2d119484bf89fe.jpg" border='0' style='float: left; margin-right: 8px; margin-bottom: 8px; margin-top: 8px" _moz_resizing="true"/>
Paulo Bueno
Did that work for some images? Can you narrow down what makes that one fail? It looks like your quotes are off and/or your are mixing html encoded characters into the styles.
rosscj2533
Well after a few tries the result is:html="<img src='http://www.domain.com/abc.gif' border='0'>"Using insertHTML the response returns without the full path of the image:<img src='abc.gif' border='0'> // wrongUsing insertImage the problem is the addictional parameters:<img src="http://www.domain.com/abc.gif border='0'" />i've tried the trick to insert a quote o pass additional parameters but the insertimage encode the string giving an wrong result:<img src="http://www.domain.com/abc.gif" border='0'"/>
Paulo Bueno
What do you mean by additional parameters? Attributes of the img tag? What you say you tried with insertImage is slightly different than insertHTML. Did you try insertImage with html="<img src='domain.com/abc.gif' border='0'>" ?
rosscj2533
Yes, I mean attributes. And insertImage only accepts the src attribute. So as your example the output would be "<img src="<img src='domain.com/abc.gif border='0'>">. What is a mess... :(
Paulo Bueno
Ah, now I see the problem. You can get a hold of that image immediately after inserting it and add the attributes. I'll update my answer with something I found.
rosscj2533
+1  A: 

In my experience, working with native rich text editors (aka div's with contentEditable="true" or iframes with designMode set to on) is very difficult. The API is inconsistent across browsers and their behavior is often unexpected and buggy. Because of this I tend to use document.execCommand() as little as possible. Instead I tend to reply on direct DOM manipulation.

With that in mind, here's how I'd try to solve the problem you described:

  • Create the an in-memory image element and set the appropriate url.
  • Find the DOM node that contains user's cursor.
  • Insert the in-memory image element into the DOM node found in the previous step.

The code needed to implement the second step is somewhat tricky and varies hugely across browsers. I'll try to post a working example in the next day or two. I hope this helps in the mean time.

Xavi
A: 

As far as I understand, and have experienced it myself, this is 1. inherent to the browser's HTML editing engine and 2. it happens only when the image that you are trying to insert, and the address you are running the HTML editor from are on the same domain.

As a solution, if your server/provider allows this, you could set up a second subdomain that points to www, for example

www2.example.com

and link to the image as

http://www2.example.com

this should have the result that the absolute link remains untouched.

upon saving the HTML, you just have to replace all occurrences of www2.example.com to www.example.com.

Another, maybe simpler, way would be to run the WYSIWYG editor on www2.example.com and inserting the proper absolute URLs.

Pekka
A: 

I think because of security reasons, you can not specify complete url such as www.example.com.

Sarfraz
A: 

I believe that you should be able to use jQuery.

You will probably want to use something along the lines of

$(rteName).find('body').html('<img src="http://www.example.com/" alt="...">

but probably with some changes to the selector(s).

jezmck