Flare turning JavaScript into CDATA

This forum is for all Flare issues related to styles, stylesheets and XML.
Post Reply
kathryngz
Propeller Head
Posts: 75
Joined: Wed May 14, 2014 11:31 am

Flare turning JavaScript into CDATA

Post by kathryngz »

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>
NorthEast
Master Propellus Maximus
Posts: 6359
Joined: Mon Mar 05, 2007 8:33 am

Re: Flare turning JavaScript into CDATA

Post by NorthEast »

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).
kathryngz
Propeller Head
Posts: 75
Joined: Wed May 14, 2014 11:31 am

Re: Flare turning JavaScript into CDATA

Post by kathryngz »

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

Re: Flare turning JavaScript into CDATA

Post by kathryngz »

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.
Nita Beck
Senior Propellus Maximus
Posts: 3667
Joined: Thu Feb 02, 2006 9:57 am
Location: Pittsford, NY

Re: Flare turning JavaScript into CDATA

Post by Nita Beck »

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
RETIRED, but still fond of all the Flare friends I've made. See you around now and then!
kathryngz
Propeller Head
Posts: 75
Joined: Wed May 14, 2014 11:31 am

Re: Flare turning JavaScript into CDATA

Post by kathryngz »

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
NorthEast
Master Propellus Maximus
Posts: 6359
Joined: Mon Mar 05, 2007 8:33 am

Re: Flare turning JavaScript into CDATA

Post by NorthEast »

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.
devjoe
Sr. Propeller Head
Posts: 337
Joined: Thu Jan 23, 2014 1:43 pm

Re: Flare turning JavaScript into CDATA

Post by devjoe »

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.
kathryngz
Propeller Head
Posts: 75
Joined: Wed May 14, 2014 11:31 am

Re: Flare turning JavaScript into CDATA

Post by kathryngz »

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 pasted 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
Post Reply