Parsing a string returned from ckeditor using javascript string methods -
i trying area of data out ckeditor. in order use following code
function get_body_html(){ var email = ckeditor.instances['message'].getdata(); var before_body = header_to + + to_subject + subject + subject_body; var s_index = email.indexof(before_body)+before_body.length; var e_index = email.indexof(body_footer); return email.substring(s_index,e_index); }
for reason works when on page load
ckeditor.instances.message.setdata(header_to + + to_subject+ subject + subject_body + body_text + body_footer); get_body_html();
it works correctly , gives me same string contained in body_text.
but when this
body_text = get_body_html(); ckeditor.instances.message.setdata(header_to + + to_subject + subject + subject_body + body_text + body_footer);
in onclick function gets wrong indexs somehow. can't find string , returns -1 other times gets weird index doesn't make sense. these index variations happen when code changed tackle problem different way. if wrong indices -5 , 2 continue wrong indices until made code change.
there 2 facts should know editor.setdata
.
- in cases asynchronous (it depends on type of editor). that's why accepts callback. therefore code meant executed after
setdata()
should executed in callback. - it never asynchronous before editor ready. in period (between editor initialization ,
instanceready
event) works in different mode - caches set value , ongetdata()
returns value.
so, see on page load call synchronously setdata()
, getdata()
- function works because value you're expecting get.
but then, when try getdata()
when editor ready html parsed, fixed, processed , perhaps differently formatted ckeditor. guess indexof()
checks not enough handle this. have rethink function - e.g. regexp can help.
what can removing htmlwriter
plugin, formats html in way may make harder work it. e.g.:
config.removeplugins = 'htmlwriter';
Comments
Post a Comment