Setting single line variable is simple for javascript, but not multi line. In fact, its not possible to do it unless with some additional escape characters input into the line.
Take for example
var temp = "This is Aaron"; //this works perfectly
var temp = "This\
is\
Aaron"; // this works with the /escape character
var temp = "This
is
Aaron"; // this does not work.
Now in order to solve it, I used PHP, after a bit of experiment, there are two approaches. I made used of (include + regex) and (file_get_contents + regex). Let's start with include + regex first.
var temp = "<?php $string = include('synopsis.php'); echo preg_replace("#(\r\n\n\r)#s", ' ', $string); ?>";
Inside the file: synopsis.php is a content like this:
$var = "This is Aaron";
return $var;
?>
This works perfectly fine, but its very cumbersome because you need to assign the variables and all.
I therefore came up with file_get_contents + regex. Inside the file:
var temp = "<?php $string = file_get_contents("synopsis.php"); echo preg_replace("#(\r\n\n\r)#s", ' ', $string); ?>";
Synopsis.php can contain any normal HTML, no need for customization like return.
This works great for me, what I was trying to do was to dynamically update the innerHTML variable of a DIV tag using javascript. The thing was that the content I wanted to update the DIV is multi line and with that javascript cannot work unless I manually edited each HTML page's source code to a single. This is definitely not very productive, for every change the client may want to make, i will have to edit it on Dreamweaver and go through line by line on notepad to make them a single line.
Hopefully this may help someone :)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment