Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2006-05-02 18:12:13
- mattgilbert
- Member
- Registered: 2006-03-23
- Posts: 70
txp:body tag passed to javascript in a form
i’m betting there’s an easy solution to this, but i can’t sort it out.
i’d like to access the body text of an article in javascript through the article form. for example in the form there could be:
<code><script type=“text/javascript” language=“javascript”>//<![CDATA[
alert(‘<txp:body/>’);
//]]></script></code>
but the html formatting of the body text seems to screw things up by adding linebreaks or something, because i get a javascript error something like this:
<code>Error: unterminated string literal
Source File: http://www.domain.com/
Line: 46, Column: 6
Source Code:
alert(’ <p>Just trying things out. Trying to pass this body text to javascript.</p></code>
is there a way to fix the formatting of the body text so that this doesn’t happen?
Offline
#2 2006-05-02 18:30:17
- anoke
- Archived Plugin Author
- Registered: 2006-04-15
- Posts: 152
Re: txp:body tag passed to javascript in a form
wild guess here.. Turn textile off.
But that won’t make javascript not to break when there is ‘/” and such characters in the article body I think.
You should escape those characters before they end up showing in the javascript code – and that should be done on the textpattern side :(
(edit: note to self – triple check every sentence when lacking sleep..)
Last edited by anoke (2006-05-02 19:22:04)
- When chickens are cold, they roost in trees; when ducks are cold, they plunge into water -
Offline
#3 2006-05-02 18:52:10
- mattgilbert
- Member
- Registered: 2006-03-23
- Posts: 70
Re: txp:body tag passed to javascript in a form
thanks for the reply anoke!
weird. setting textile to “Convert linebreaks” sort of works, but only when the article body has some text followed by linebreak(s), followed by nothing. So, no multiple paragraphs. This is useless, but maybe it’s a clue. Setting textile to “Leave text untouched” doesn’t work at all.
Also, just typing in <code><br></code>‘s in the article body instead of linebreaks also works, but hopefully I can avoid that.
Other ideas?
Offline
#4 2006-05-02 20:53:09
- mattgilbert
- Member
- Registered: 2006-03-23
- Posts: 70
Re: txp:body tag passed to javascript in a form
found a sloppy workaround. in a div with <code>id=“hiddenStorage”</code> and <code>visibility:hidden</code> I put an article tag that uses a form like this:
<code><txp:body />
<script type=“text/javascript” language=“javascript”>//<![CDATA[
var txt = document.getElementById(‘hiddenStorage’).innerHTML;
txt = txt.substring(0, txt.indexOf(‘<script’))+txt.substring(txt.indexOf(‘/sc’+‘ript>’)+8, txt.length);
alert(txt);
document.getElementById(‘hiddenStorage’).innerHTML = ‘’;
//]]></script></code>
So the body text is written into the div along with the javascript, then that innerHTML is put is <code>var txt</code> and the javascript is clipped out of it. In this example I only used the txt in an alert, but obviously you could do anything with it at that point. I have no idea how this will perform in all browsers, and I’m still hoping for a cleaner solution.
Offline
#5 2006-05-03 03:41:43
- Mary
- Sock Enthusiast
- Registered: 2004-06-27
- Posts: 6,236
Re: txp:body tag passed to javascript in a form
<form action="">
<input type="hidden" id="hiddenStorage" name="article_body" value="<txp:body />" />
</form>
<script type="text/javascript">
<![CDATA[
alert(document.getElementById('hiddenStorage').nodeValue);
//]]>
</script>
? :)
Offline