Can Javascript work with Multiple lines of Text.

J

jackson.rayne

Hello,

Another newbie question here.

Let me explain my situation first. I have bought a 3rd party tool
that runs a PHP script and gives me some HTML code which I can
directly use in my pages.

The code generated is normal HTML code, example

<<<<<<<<<<<<Example>>>>>>>>>>>>>>>>>>

<table class= "myclass" border="1" width="160" cellspacing="0"
cellpadding="0">
<tr>
<td width="100%">
<table border="0" width="160" cellspacing="0" cellpadding="1"><tr>
<td width="55" valign="top" align="center">

<<<<<<<<<<<<Example Ends>>>>>>>>>>>>>>

Now all the above code is contained in a PHP variable. When I want to
display the above code on my site, all I have to do is insert

%%Variable_Name%%

and it will embed the whole code on my webpage.

Now here's what I want. I need to make some changes in the above HTML
code using Javascript. In particular I want to search for some value
and make replacements.

Since the above code contains text spread across multiple lines, I
don't know whether it would work with Javascript. All I want to do is
put the above code (text) in a Javascript variable and make some
changes. I tried but it is not working.

I contacted the developer but he says that since his tool is being
used by large number of people he can't make changes based on just one
request.

Any ideas on how to proceed ahead..

Thanks in advance,
Rayne
 
G

Gregor Kofler

(e-mail address removed) meinte:
Since the above code contains text spread across multiple lines, I
don't know whether it would work with Javascript. All I want to do is
put the above code (text) in a Javascript variable and make some
changes. I tried but it is not working.

Tried what? What is "not working"? Show either your efforts, or make
clear what you want to achieve.

Gregor
 
H

Henry

Hello,

Another newbie question here.

Let me explain my situation first. I have bought a 3rd
party tool that runs a PHP script and gives me some HTML
code which I can directly use in my pages.

The code generated is normal HTML code, example

<<<<<<<<<<<<Example>>>>>>>>>>>>>>>>>>

<table class= "myclass" border="1" width="160" cellspacing="0"
cellpadding="0">
<tr>
<td width="100%">
<table border="0" width="160" cellspacing="0" cellpadding="1"><tr>
<td width="55" valign="top" align="center">

<<<<<<<<<<<<Example Ends>>>>>>>>>>>>>>

Opinions on how 'normal' that HTML is may vary.
Now all the above code is contained in a PHP variable.
When I want to display the above code on my site, all
I have to do is insert

%%Variable_Name%%

and it will embed the whole code on my webpage.

Now here's what I want. I need to make some changes in the
above HTML code using Javascript.

Why not PHP?
In particular I want to
search for some value and make replacements.

PHP can't do that?
Since the above code contains text spread across multiple
lines,

If that is the case all it rally means is that the sequence of
characters in the PHP variable includes characters that are line
terminators.
I don't know whether it would work with Javascript.

Javascript string values may also be sequences of characters that
include line terminators.
All I want to do is put the above code (text) in a
Javascript variable

So you mean to output javascript source code where the contents of the
PHP variable are the contents of a javascript string literal that is
assigned to a variable? That is where your problem appears; javascript
string literals are forbidden from including line terminators in there
source code. In order to get line terminator characters into the
resulting sequined of characters defined by a string literal the line
terminator characters must be represented differently; by an escape
sequence. For example, the line feed (new line) character may be
represented by the escape sequences \n, \x0A, and \u000A (note that
all escape sequences start with the backslash character) (the last two
of these are hex and Unicode escape sequences that use hexadecimal
representations of the character code to indicate which character will
be substituted for them in the string value that will result form a
string literal in which they appear).
and make some changes. I tried but it is not working.

I contacted the developer but he says that since his tool
is being used by large number of people he can't make
changes based on just one request.

Any ideas on how to proceed ahead..

In web development it is normal to be inserting arbitrary text into
various contexts, and so it is normal to have to process that text and
'escape' it for the context into which it will be inserted. This is so
normal that PHP (and pretty much every other web development tool) has
a set of 'escaping' functions/methods, one of which may be
appropriate. And if not it will be worth your while to write one as
you would need it again and again.

In the context of inserting text into a javascript string the
characters that should be escaped are the line feed, carriage return,
line separator (\u2028), paragraph separator (\u2029), backspace, tab,
vertical tab, from feed and backslash, with at least one of double
quote and single quote also needing escaping depending on which type
of quote is used around the string literal (generic functions should
convert both unconditionally as that is safest).

A bug in IE/JScript means that the simple escape sequence of vertical
tab: \v, does not work (is considered a literal 'v' character) so hex
or Unicode escape sequences (\x0B, \u000B) should be used for vertical
tab, but as the escaping is likely to be done based on the character
codes in the original it could be simplest to use Unicode escape
sequences for every significant character. Because the backslash
character is used in escape sequences the escaping process must escape
it first if it performed using some sort of text replacing function
(and the reversing of the process must unescape it last) else the
previously substituted escape sequences end up becoming literal
character sequences.
 
J

Jorge

Since the above code contains text spread across multiple lines, I
don't know whether it would work with Javascript. All I want to do is
put the above code (text) in a Javascript variable and make some
changes. I tried but it is not working.

An idea:
javascript:txt= document.documentElement.innerHTML;alert(txt);

or two:
javascript:txt= document.body.innerHTML;alert(txt);

But I think you're heading in the wrong direction...
 
Y

Yanick

Hello,

Hello.


Let me explain my situation first. I have  bought a 3rd party tool
that runs a PHP script and gives me some HTML code which I can
directly use in my pages.

The code generated is normal HTML code, example
[snip]

Ok. Understand what's first, first.

1) HTML = HyperText Markup Language; PHP = PHP Hypertext Processor. So
here we go:

If you have the PHP extension installed unto a web server, it means
that the server, as it reads the requested file from the client, that
file will be caught by the PHP parser and execute any script it finds,
replace the script portion of the file by whatever output it produces,
and the server will send that portion instead of the PHP script. (This
is the same thing with any other server-side scripting language. (I'm
explaining briefly here.)

For example, if your equest the file hello.php, and the file looks
like this:

<html>
<head>
</head>
<body>
<?php echo '<p id="hello">Hello world!</p>'; ?>
</body>
</html>

You will receive:

<html>
<head>
</head>
<body>
<p id="hello">Hello world!</p>
</body>
</html>

Now, PHP doesn't understand the HTML part of you page. In fact, it
doesn't know it at all. All it knows (in this example anyway) is that
there's a string that contains '<p id="hello">Hello world!</p>', and
that it outputs it. Thus, PHP can replace portion of the strings it
knows BEFORE it sends it to the client. But if you want to modify the
HTML only from the client side, THEN you make use of Javascript. So,
let's change the code a little:

<html>
<head>
</head>
<body>
<?php echo '<p id="hello">Hello world!</p>'; ?>
<script type="text/javascript">
var el = document.getElementById("hello");
el.innerHTML += "<br />Bye world!";
</script>
</body>
</html>

You will receive from the server this file:

<html>
<head>
</head>
<body>
<p id="hello">Hello world!</p>
<script type="text/javascript">
var el = document.getElementById("hello");
el.innerHTML += "<br />Bye world!";
</script>
</body>
</html>

NOTE : the <script> tag is still there! That's because it has not yet
been executed! But first, the browser (IE/Firefox/etc.) has to parse
that data too. This is because HTML is markup language, and the
browser converts the markups into the DOM (Data Object Model); the DOM
is organized in a tree-like structure in the memory of the browser.
So, this is why everything is attached to the document object in
javascript. (Because Javascript talks to the DOM, not the HTML.) In
your example, the DOM would look like this:

object:window
...DOMElement:body
.....DOMElement:p{id:"hello"}
.......textElement:Hello world!

NOTE : this is a very simplified representation of the DOM tree, for
explanation purposes.

As you may have noticed, the <script> tag is not there. This is
because, as PHP scripts are executed as they are encountered,
Javascript is the same thing. The browser executes the scripts, and
store whatever it tells it to store in memory. In our example, the
browser now has to parse the <script> tag of the file it received,
having the DOM tree above. When executed, the script makes a requests
to the browser to find the element with an id of "hello". The Browser
returns the <p> element with every nodes it contains (here, it's a
text node "Hello world!"

NOTE : if a script request an element that has not yet been added to
the DOM, the browser will return 'undefined'. This is why you cannot
directly access elements below the <script> tags.

Then, the script tells the browser to add HTML content to this <p>
element. The DOM now becomes :

object:window
...DOMElement:body
.....DOMElement:p{id:"hello"}
.......textElement:Hello world!
.......DOMElement:br
.......textElement:Bye world!

Et voilà!

NOTE : when you use innerHTML in your Javascript, the browser has to
parse the data set to the element. If you can read the value, it's
simply because the browser holds the string that it used to parse the
data when it received the file from the server. It doesn't really use
the data itself for processing beyond the parsing.

In conclusion, the best way to replace data in your page is :

1) the the parent element of your <table> and get it's innerHTML, then
modify it. (Remember, every time you change innerHTML, the browser has
to parse it, so I suggest you use a variable to do what you want, then
reassign the value to the innerHTML.)

2) directly get the elements you want with the DOM methods :
document.getElementById(id) or document.getElementsByTagName(tag) (the
last method returns an array regardless. Be advised!)

3) modify the PHP strings before the script outputs them to the
client.


I hope this was clear enough, and helpful. :)

yanick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top