PDA

View Full Version : Dynamic Ajax Content to fill a textarea



kirpi
08-17-2006, 10:42 PM
Script: Dynamic Ajax Content
http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm

Is there a way to let the script fill a textarea, instead of a general "rightcolumn" div?

I plan to use this most useful script to retrieve text from a knowledgebase and send emails.
For this reason I need the contents to be inserted into a textarea.

Thank you
Luigi

P.S. Yes, I just plan to use plain text, so I would not bother about images and formatting.

jscheuer1
08-18-2006, 07:55 AM
All other things being equal, that is assuming that you can retrieve the content you desire, this is where the contents of the external page are actually written to the 'top' page:


function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

Specifically, the red line. Now, you can give your textarea an id and access it in that same manner as a division but, a textarea doesn't have an innerHTML object, or if it does, it isn't the same type of thing. What a textarea does have is its value attribute which represents/holds/'stands for' the text it contains. So, you could do this:


document.getElementById(containerid).value=page_request.responseText

kirpi
08-18-2006, 10:35 AM
Excellent!
It does work, smooth and fine.
Thank you.

The only issue (one I did not foresee, ignorant as I am) is that now the whole html page is loaded into the textarea, and not just the text as it was originally.

Is there any way, please, to strip all away, and keep just the real content?

Luigi

DimX
08-18-2006, 11:12 AM
Well you can first load the respons into an invisible div and then load the innerText of the div into the textarea.

kirpi
08-18-2006, 04:04 PM
> Well you can first load the respons into an invisible div
> and then load the innerText of the div into the textarea.

Please, how that would translate, in practical terms?
Could you please show an example?

Here is the page I'm working on http://www.kirpi.it/kirpimail/ajaxcars/index4 (still a rough, of course).
As you see, I add (instead of just loading) page after page, click by click.
This will eventually allow to gather text from a wiki knowledgebase, which in turn is online and open to all.

Luigi

jscheuer1
08-18-2006, 05:21 PM
I'd use a regEx replace like so:


document.getElementById(containerid).value=page_request.responseText.replace(/<[^>]*>/g, '');

This will strip all tags that begin and end like:


< . . . >

kirpi
08-20-2006, 11:13 AM
> I'd use a regEx replace...
> [which] will strip all tags that begin and end like: < . . . >

Yes, that would be a 90% solution.
Just I noticed that at times thing are messed up, as it's not smart with spaces and line breaks.

Luigi

DimX
08-20-2006, 11:44 AM
Please, how that would translate, in practical terms?
Could you please show an example?


function loadpage(page_request, containerid){
if(page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById("invisiblediv").innerHTML = page_request.responseText;
document.getElementById(containerid).value = document.getElementById("invisiblediv").innerText;
}


You must have a div with style="display:none" and id="invisiblediv" on your page.

But using replace() is probably still better.

jscheuer1
08-20-2006, 06:41 PM
> I'd use a regEx replace...
> [which] will strip all tags that begin and end like: < . . . >

Yes, that would be a 90% solution.
Just I noticed that at times thing are messed up, as it's not smart with spaces and line breaks.

Luigi

I'm having trouble understanding how spaces would be a problem, though multiple spaces could be treated a certain way if desired. Line breaks, it depends. Do you mean that it is not picking up tags which span line breaks, or that it isn't inserting line breaks where they existed in the formatted HTML? For the former, add the m switch, for the latter use another replace function first to replace <br> with \n and/or format your HTML that you are replacing so that it relies on <br> tags for all important line breaks and/or has actual line breaks where desired.