Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-04-10 06:50:32

azw
Member
Registered: 2007-01-29
Posts: 279

How to escape </a> in JavaScript write statement with txp:section tag

I’ve been using JavaScript in the footer of a site to obfuscate an email address. Because the email address is just one element on a line of text, I’ve had to use JavaScript to write the entire line.

This works fine, except for one problem: the closing tag, </a>, in the link to the copyright page doesn’t get escaped.

I would hard code the link, but I’m using the MLP (Multi-Lingual Publishing Pack). In other words, the link and the name of the section both change depending on which language is currently being viewed.

Is there a way to fix this?

Here’s a simplified example of the code. I’ll mark the key line with “>>>”

<script type="text/javascript">
//<![CDATA[

window.document.write("<ul>");
    window.document.write("<li>");


>>>    window.document.write('<txp:section name="copyright" link="1" title="1" />');


    window.document.write(" &copy; YEAR-");
    window.document.write("<txp:upm_datetime format='%Y' />");
    window.document.write("<\/li>");

window.document.write("<li>SITE NAME<\/li><li>SITE OWNER<\/li><li class='last'>");
<txp:alt_obfuscate email="EMAIL ADDRESS" scripttags="none" />
window.document.write("<\/li><\/ul>");
//]]>
</script>

Last edited by azw (2010-04-10 06:51:21)

Offline

#2 2010-04-10 10:21:53

trenc
Plugin Author
From: ⛵️, currently Göteborg, SE
Registered: 2008-02-27
Posts: 574
Website GitHub

Re: How to escape </a> in JavaScript write statement with txp:section tag

var text = '<txp:section link="1" title="1" />';
window.document.write(text.replace(new RegExp(/\//gi), '\/'));

If you already use jQuery you can do something like this, jQuery do escaping for you:

<script type="text/javascript">
//<![CDATA[
$(function() {
  var li_1 = '<li><txp:section name="copyright" link="1" title="1" /> &copy; YEAR-<txp:posted format='%Y' /></li>';
  var li_2 = '<li>SITE NAME</li>';
  var li_3 = '<li>SITE OWNER</li>';
  var li_4 = '<li class="last"><txp:alt_obfuscate email="EMAIL ADDRESS" scripttags="none" /></li>';
  $('#footer').append('<ul>' + li_1 + li_2 + li_3 + li_4 + '</ul>');
});
//]]>
</script>

Last edited by trenc (2010-04-10 10:33:16)


Digital nomad, sailing the world on a sailboat: 32fthome.com

Offline

#3 2010-04-11 09:09:58

azw
Member
Registered: 2007-01-29
Posts: 279

Re: How to escape </a> in JavaScript write statement with txp:section tag

Thanks, Trenc, for your reply.

I’m not having luck yet.

I tried to put this code in an external js file:

function xx_section_prep()
{
	var xx_result_text;
	<txp:if_section name="copyright">
		xx_result_text = '<txp:section title="1" name="copyright" />';
	<txp:else />
		xx_result_text = '<txp:section link="1" title="1" name="copyright" />';
	</txp:if_section>
	return xx_result_text.replace(new RegExp(/\//gi), '\/');
}

And I put this line appeared in the textpattern form for the footer:

   window.document.write(xx_section_text);

That created an error: “Invalid XML namespace”. This was a fatal error, so the javascript stopped running. I wasn’t able to figure out what causes this problem.

If I put it all in the form, this line still causes a warning because it creates an unescaped “/” in the javascript.

xx_section_text = '<txp:section link="1" title="1" name="copyright" />';
	</txp:if_section>

Of course, it’s not a fatal error, just a warning, so it still works. But that’s the same result I was getting with the original code.

Any other ideas?

I may have to learn jQuery to fix the problem.

Last edited by azw (2010-04-11 09:11:22)

Offline

#4 2010-04-11 10:44:23

trenc
Plugin Author
From: ⛵️, currently Göteborg, SE
Registered: 2008-02-27
Posts: 574
Website GitHub

Re: How to escape </a> in JavaScript write statement with txp:section tag

Hi azw,

I tried to put this code in an external js file:

function xx_section_prep()
{
        var xx_result_text;
        <txp:if_section name="copyright">
                xx_result_text = '<txp:section title="1" name="copyright" />';
        <txp:else />
                xx_result_text = '<txp:section link="1" title="1" name="copyright" />';
        </txp:if_section>
        return xx_result_text.replace(new RegExp(/\//gi), '\/');
}

That created an error: “Invalid XML namespace”.

Yes that’s normal, The TXP tags (<txp:if_section />) are not being parsed outside of textpattern, here you have placed it in an external javascript file.

But beside that, that regex replace I wrote above is useless and wrong.
The regex have to be done before. Before the </a> will be first shown in the javascript and not before rendering in HTML, my fault.

So you can split the link in parts with generated href attribute and anchor value and the escape some string (like <, / and >) – as the google analytics code look like.

Or, which I prefer: You have to do real DOM scripting, for an easy approach with the help of jQuery.


Digital nomad, sailing the world on a sailboat: 32fthome.com

Offline

#5 2010-04-11 18:31:57

azw
Member
Registered: 2007-01-29
Posts: 279

Re: How to escape </a> in JavaScript write statement with txp:section tag

I appreciate your help!

… split the link in parts with generated href attribute and anchor value and the escape some string …

Exactly. When I used version 4.0.6, I had broken the code down so I could create my own links. That worked, but no longer works with 4.2.0.

I’ll paste that below, putting the link part between >>> and <<<.

Do you know why it no longer works? I don’t think article_custom has changed.

The form simple_permlink contains just one line:

<txp:permlink />

Here’s the old code.

<script type="text/javascript">
//<![CDATA[
window.document.write("<ul>");

>>>
    window.document.write("<li><a href='");
    window.document.write("<txp:article_custom section='copyright' limit='1' form='simple_permlink' />");
    window.document.write("'>");
<<<

    window.document.write("<txp:section name='copyright' title='1' />");
    window.document.write("<\/a> &copy; YEAR-");
    window.document.write("<txp:upm_datetime format='%Y' />");
    window.document.write("<\/li>");

    window.document.write(" &copy; YEAR-");
    window.document.write("<txp:upm_datetime format='%Y' />");
    window.document.write("<\/li>");

window.document.write("<li>SITE NAME<\/li><li>SITE OWNER<\/li><li class='last'>");
<txp:alt_obfuscate email="EMAIL ADDRESS" scripttags="none" />
window.document.write("<\/li><\/ul>");
//]]>
</script>

Offline

#6 2010-04-12 09:13:42

trenc
Plugin Author
From: ⛵️, currently Göteborg, SE
Registered: 2008-02-27
Posts: 574
Website GitHub

Re: How to escape </a> in JavaScript write statement with txp:section tag

Hm weird, your code does work here.

Are you sure the the simple_permlink form set as article type?


Digital nomad, sailing the world on a sailboat: 32fthome.com

Offline

#7 2010-04-13 07:23:16

azw
Member
Registered: 2007-01-29
Posts: 279

Re: How to escape </a> in JavaScript write statement with txp:section tag

Hmm. It was “misc.” It’ll take me a couple of days, but I’ll test to see if that fixes the original problem. Boy, that would be simple! Thanks, again!

Offline

#8 2010-04-13 07:41:39

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: How to escape </a> in JavaScript write statement with txp:section tag

trenc wrote:

Are you sure the the simple_permlink form set as article type?

Would be nice indeed, but the type is just used by the tag builder, override form and backend’s form grouping. There is no check or requirement for the type.

Btw, might be that the article_custom is indeed unrequired. The permlink nowdays has id attribute.

window.document.write("<txp:permlink id="1" />");

What comes to the single quotes, you don’t need them. The JavaScript isn’t TXP markup, neither the JS is going to see server side language. But, single quotes is a feature in the TXP’s parser and you don’t want to use them if not required, as using single quotes invokes parser.

Offline

#9 2010-04-13 08:13:02

azw
Member
Registered: 2007-01-29
Posts: 279

Re: How to escape </a> in JavaScript write statement with txp:section tag

Gocom wrote:

…. Btw, might be that the article_custom is indeed unrequired. The permlink nowdays has id attribute.

window.document.write("<txp:permlink id="1" />");

That’s interesting. This site uses the MLP (Multi-Lingual Publishing Pack). I was assuming that because the “rendition” changes depending which language is being viewed, I can’t identify a specific article. Is that wrong?

Offline

#10 2010-04-13 08:50:48

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: How to escape </a> in JavaScript write statement with txp:section tag

azw wrote:

I was assuming that because the “rendition” changes depending which language is being viewed, I can’t identify a specific article.

You should be able to call articles like you do with out MLP. MLP just redirects all database queries accesing textpattern table to right language record depending on the language the user is viewing. That is basically why MLP requires a modification to txplib_db.php.

Offline

#11 2013-11-14 06:26:23

azw
Member
Registered: 2007-01-29
Posts: 279

Re: How to escape </a> in JavaScript write statement with txp:section tag

This issue has reappeared with an upgrade of Txp. For some reason, calling for a link with this code didn’t work:

 window.document.write("<txp:section link='1' title='1' name='copyright' />");

But I had success by setting a varialbe the variable tag and then calling it in the write function, like this:

<script type="text/javascript">
//<![CDATA[

<txp:variable name="copyright-link" value="<txp:section link='1' title='1' name='copyright' />" />

/* if the current page is the copyright page */
window.document.write("<ul>");
  <txp:if_section name="copyright">
    window.document.write("<li>");
    window.document.write("<txp:section title='1' name='copyright' />");
    window.document.write(" &copy; 2005-");
    window.document.write("<txp:upm_datetime format='%Y' />");
    window.document.write("<\/li>");


/* if the current page is anything but the copyright page */
  <txp:else />
    window.document.write("<li>");
    window.document.write('<txp:variable name="copyright-link" />');
    window.document.write(" &copy; 2005-");
    window.document.write("<txp:upm_datetime format='%Y' />");
    window.document.write("<\/li>");
  </txp:if_section>

etc.

That was easy!

Offline

Board footer

Powered by FluxBB