Flare turning JavaScript into CDATA

This forum is for all Flare issues related to styles, stylesheets and XML.

Flare turning JavaScript into CDATA

Postby kathryngz on Thu Jan 31, 2019 11:01 am

Hi all,

I'm trying to use XML on a Flare page with JavaScript, similar to this example on W3Schools:
https://www.w3schools.com/xml/ajax_applications.asp.

The target is HTML5. I've done this type of thing successfully multiple times on non-Flare websites.

But for some reason, Flare is turning this:

Code: Select all
<script type="text/javascript" src="Resources/scripts/featured_resource.js">
show_resource(11);
</script>


into this:

Code: Select all
<script type="text/javascript" src="Resources/scripts/featured_resource.js">/* <![CDATA[ */
show_resource(11); /* ]]> */
</script>


Of course the code doesn't work when it's commented out :cry:

I started out with all the code in the Flare page between script tags, but it did the CDATA thing. Then I put functions in the external file and called the main one from the web page as shown above--same problem. I even tried putting the function call in the external file, but then nothing happened.

I'm really puzzled as to why Flare is turning the code into CDATA and comments to boot. Any idea how I can get this to work properly? It's holding up an important project.

Thanks in advance--

Kathryn

P. S. Here is the code in the external .js file:

Code: Select all
function show_resource(i) {
   var xmlhttp = new XMLHttpRequest();
   xmlhttp.onreadystatechange = function() {
         if (this.readyState == 4 && this.status == 200) {
            myFunction(this, i);
         }
      };
   xmlhttp.open("GET", "resources.xml", true);
   xmlhttp.send();
}

function myFunction(xml, i) {
   var xmlDoc = xml.responseXML;
   x = xmlDoc.getElementsByTagName("resource");
   document.getElementById("featured_resource").innerHTML =
      x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
}


And here's a representative sample from the XML file (for readability, I've shortened some of the text):

Code: Select all
<resources>
   <resource>
      <type>presentation</type>
      <date></date>
      <title>Answers Hiding in Plain Sight: Using What You Know to Discover What You Don't Know</title>
      <description>Ever feel like you're looking into a genealogical dark tunnel....</description>
      <webinar_link>https://...</webinar_link>
      <presentation_link>https://...</presentation_link>
      <other_link></other_link>
      <tags>research, timeline grid</tags>
   </resource>
</resources>
kathryngz
Propeller Head
 
Posts: 52
Joined: Wed May 14, 2014 11:31 am

Re: Flare turning JavaScript into CDATA

Postby Dave Lee on Fri Feb 01, 2019 1:09 am

It's not the CDATA that is causing the problem.

It doesn't work because your script tag has both a link to a js file source (scr="..."), and script within the tag - but it can only have either one of these, not both. I think it will open the linked script file, but ignore the script inside the tag.
https://www.w3schools.com/tags/tag_script.asp

It'll work if you split it into two script tags, i.e. the one with src="..." first, followed by the one with your show_resource script (this needs to come second as it references a function in your script file).
Dave Lee
Master Propellus Maximus
 
Posts: 5467
Joined: Mon Mar 05, 2007 8:33 am
Location: UK

Re: Flare turning JavaScript into CDATA

Postby kathryngz on Fri Feb 01, 2019 8:27 am

Dave, that solved the problem--thank you so much! :D :D :D
kathryngz
Propeller Head
 
Posts: 52
Joined: Wed May 14, 2014 11:31 am

Re: Flare turning JavaScript into CDATA

Postby kathryngz on Fri Feb 01, 2019 12:24 pm

I just learned something else that I wanted to post here, in case it's helpful. I made some changes to my JavaScript file, and suddenly Flare changed the function call into CDATA again.

Turns out I had a syntax error in the changes. As soon as I corrected that, the CDATA problem went away.

Apparently, turning script into CDATA is Flare's way of signaling some type of problem with the script.
kathryngz
Propeller Head
 
Posts: 52
Joined: Wed May 14, 2014 11:31 am

Re: Flare turning JavaScript into CDATA

Postby Nita Beck on Fri Feb 01, 2019 1:20 pm

Something just occurred to me. Another Flare consultant once mentioned to me that when he edited Javascripts in Flare's internal text editor, the scripts got corrupted. He recommended that one should always use some other text editor outside of Flare. Just sayin', in case this has any bearing on your issue. (If you mentioned that above, my apology. It's Friday and I'm brain-weary.)
Nita
Image
Content Strategist / Information Architect / Flare Consultant: http://beck-communications.com
Manager, Rochester Flare User Group: http://www.facebook.com/RochesterFlareUserGroup
Nita Beck
Senior Propellus Maximus
 
Posts: 3107
Joined: Thu Feb 02, 2006 9:57 am
Location: Pittsford, NY

Re: Flare turning JavaScript into CDATA

Postby kathryngz on Fri Feb 01, 2019 2:57 pm

Nita, that's good to know, and I hadn't mentioned it! Thanks for that insight. Every bit of knowledge helps :)

Enjoy the rest of your Friday! :)

Kathryn
kathryngz
Propeller Head
 
Posts: 52
Joined: Wed May 14, 2014 11:31 am

Re: Flare turning JavaScript into CDATA

Postby Dave Lee on Mon Feb 04, 2019 12:25 am

kathryngz wrote:Apparently, turning script into CDATA is Flare's way of signaling some type of problem with the script.


Flare will enclose all scripts in CDATA (that you insert in topics).

CDATA is "character data", which is treated as text rather than parsed as XHTML, so you don't need to escape special characters.

Nita Beck wrote:Another Flare consultant once mentioned to me that when he edited Javascripts in Flare's internal text editor, the scripts got corrupted.


Flare had a bug where if you copy/paste scripts it would strip out the "$" character. Not sure if that's fixed yet.
Dave Lee
Master Propellus Maximus
 
Posts: 5467
Joined: Mon Mar 05, 2007 8:33 am
Location: UK

Re: Flare turning JavaScript into CDATA

Postby devjoe on Mon Feb 04, 2019 12:09 pm

The issue with inline scripts is that they often include < and > symbols which would be taken by HTML as tags. The CDATA is a clever trick that makes the two syntaxes compatible with one another. In:
Code: Select all
<script type="text/javascript" src="Resources/scripts/featured_resource.js">/* <![CDATA[ */
show_resource(11); /* ]]> */
</script>

You see that /* <![CDATA[ */ and /* ]]> */ are inserted around the code of the script.
/* comment text here */ is a javascript comment that ignores the contents. So the tags which make up the start and end of the CDATA syntax are ignored by the Javascript.
Meanwhile, the CDATA construct makes all its contents (between <!CDATA[ and ]]>) not be parsed as tags. The /* and */ outside the CDATA aren't anything special to HTML, so it works.

This is standard procedure for inline Javascript. An alternative is using an HTML comment which looks like <!-- comment here --> so you will also see /* <!-- */ javascript code /* --> */
When the javascript is in a separate file, nothing is parsing it as HTML and this wrapper is not needed.
devjoe
Sr. Propeller Head
 
Posts: 276
Joined: Thu Jan 23, 2014 1:43 pm

Re: Flare turning JavaScript into CDATA

Postby kathryngz on Tue Feb 12, 2019 11:17 am

But what's interesting is that as soon as I fix the problem in the XML file, the CDATA is no longer there when I view the code in the browser inspector. Instead, I see parsed content.

Something else interesting: it appears that the Flare internal editor adds some kind of non-visible characters or code to the XML that makes it fail. I had an XML file working, then edited it in the internal editor, and then it failed for no reason I could determine.

So I opened an earlier (working) copy of the XML file in Notepad. I also copied the new nodes that apparently made the file fail into Notepad. Then I copied the new nodes into the old working copy, published it, and the file no longer failed. My theory is that copying the new nodes into Notepad stripped out whatever Flare added that was causing the problem.

I haven't had any further problems since I stopped using the Flare internal editor to edit the XML file.

If I learn anything else, I'll post it here.

Thanks, everyone, for the discussion!

Kathryn
kathryngz
Propeller Head
 
Posts: 52
Joined: Wed May 14, 2014 11:31 am


Return to Styles, Stylesheets and XML

Who is online

Users browsing this forum: No registered users and 1 guest