Advertisement

05.16.2008 at 09:01PM PDT, ID: 23410223
[x]
Attachment Details

Formatting Generated Javasccript

Asked by basiclife in PHP Scripting Language, JavaScript

Hi,

I'm pulling some data from my database and using it in a javascript function call generated by PHP.

If I pass in a simple alphanumeric string, it all works fine but when I start using carriage returns, my function to escape unusual characters seems to be failing. (I copied some text from a random site to stress-test it)

Below you can see the JavaSafe() function, the code I'm using to write the event (I know this is a bad way to add handlers, but I'm just testing to get it to work).

The output from JavaSafe is formatted to replace newlines with <p> tags.

You can also see the input text and the generated output.

When firefox gets to this point, it generates an "unterminated string literal", ie reports "unterminated string constant" for this line and a "missing ')'" for the close > of the element.

I know how hacky the code is, but i've been chopping and changing for hours. Would someone please point out my glaring mistake?

Thanks in advance,

Basiclife

p.s. I'm not sure whether I want to escape < and > but if I don't, they seem to cause JS a headache.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
Input Text:
 
There are houses built in trees and then there are treehouses. Last year, we had one of our first encounters with a home literally made from trees, using the art of weaving (and sometimes grafting) trees together to form structures  a practice ecological designer, Richard Reames, called "Arcorsculpture." The Fab Tree Hab was one of the design entries for the
Index: awards, emerging from the genius of a crew including MIT architect Mitchell Joachim and our friend, Javier Arbona of Archinect. The project description emphasized consideration of whole systems (and ecosystems) in creating a truly sustainable built environment, rather than a piecemeal approach that could yield uncertain longterm outcomes.
This is not exactly garden variety as building strategies go, but its certainly among the most ornate, natural, and "green." German landscape architect, Rudolf Doernach, used techniques like this in what he broadly called "biotecture" or "agritecture." Like permaculture, these methods are set up to be largely self-sustaining, meaning that once the initial planting and early training of the branches is complete, the structures continue to grow on their own, requiring minimal external energy while providing maximum agricultural yield (as in the Fab Tree Hab, which is meant to provide food for the inhabitants). Permaculture is also about inclusion, accessibility, and mutual service between humans and the natural world. With proper knowledge, you should be able to grow your own house!
As the Australian Rainforest Information Centre points out, these are the ultimate in low-cost, low-maintenance, zero-energy homes:
"Doernachs creations produce incredible savings compared to inert construction/insulation materials and have great potential for employment, given that say, 10 million homes have 100,000 hectares of plantable surface suitable for food cultivation. Insulation, energy-savings, noise-reduction, dust suppression, carbon dioxide conversion, oxygen production and psychological benefits are all positive by-products of planted walls."
 
 
<?php
 
	//Function to format any string to be inserted into Javascript
	function JavaSafe($val) {
		$val = str_replace("\\", "\\\\", $val);
		$val = str_replace("\'", "\\\'", $val);
		$val = str_replace("\"", "\\\"", $val);
		$val = str_replace("\t", "\\t", $val);
		$val = str_replace("\r", "\\r", $val);
		$val = str_replace("\n", "\\n", $val);
		$val = str_replace("<", "&lt;", $val); // Not sure if I need these
		$val = str_replace(">", "&gt;", $val); // ^
		return $val;
	}
 
	//print the onmousemove event - It's a little messy I'm afriad.
	print "onmousemove=\"ShowImageInfo(this, '" . JavaSafe($Image->Title) . "', '" . JavaSafe("<p>" . str_replace("\n", "</p><p>", $Image->Description) . "</p>") . "');\"";
?>
 
Sample Output:
	onmousemove="ShowImageInfo(this, 'Garden House', '&lt;p&gt;There are houses built in trees and then there are treehouses. Last year, we had one of our first encounters with a home literally made from trees, using the art of weaving (and sometimes grafting) trees together to form structures  a practice ecological designer, Richard Reames, called \"Arcorsculpture.\" The Fab Tree Hab was one of the design entries for the&lt;/p&gt;&lt;p&gt;Index: awards, emerging from the genius of a crew including MIT architect Mitchell Joachim and our friend, Javier Arbona of Archinect. The project description emphasized consideration of whole systems (and ecosystems) in creating a truly sustainable built environment, rather than a piecemeal approach that could yield uncertain longterm outcomes.&lt;/p&gt;&lt;p&gt;This is not exactly garden variety as building strategies go, but its certainly among the most ornate, natural, and \"green.\" German landscape architect, Rudolf Doernach, used techniques like this in what he broadly called \"biotecture\" or \"agritecture.\" Like permaculture, these methods are set up to be largely self-sustaining, meaning that once the initial planting and early training of the branches is complete, the structures continue to grow on their own, requiring minimal external energy while providing maximum agricultural yield (as in the Fab Tree Hab, which is meant to provide food for the inhabitants). Permaculture is also about inclusion, accessibility, and mutual service between humans and the natural world. With proper knowledge, you should be able to grow your own house!&lt;/p&gt;&lt;p&gt;As the Australian Rainforest Information Centre points out, these are the ultimate in low-cost, low-maintenance, zero-energy homes:&lt;/p&gt;&lt;p&gt;\"Doernachs creations produce incredible savings compared to inert construction/insulation materials and have great potential for employment, given that say, 10 million homes have 100,000 hectares of plantable surface suitable for food cultivation. Insulation, energy-savings, noise-reduction, dust suppression, carbon dioxide conversion, oxygen production and psychological benefits are all positive by-products of planted walls.\"&lt;/p&gt;');"
[+][-]05.16.2008 at 11:06PM PDT, ID: 21588144

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.17.2008 at 01:56PM PDT, ID: 21590731

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.17.2008 at 02:01PM PDT, ID: 21590739

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.18.2008 at 10:23PM PDT, ID: 21595376

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP Scripting Language, JavaScript
Sign Up Now!
Solution Provided By: bansidhar
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.19.2008 at 12:41AM PDT, ID: 21595766

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.24.2008 at 06:09PM PDT, ID: 21640820

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906