inserting text into HTML

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a big list of HTML files, which need to be updated with a
common text.

<script language=JavaScript

I need to add the above line in each of the html files, before the
text "<\head>". All the HTML files have this text and I think some
kind of search on this string and either replacing "</head>" with the
text above and appending "<\head> to it, might be one way to do it or
just find the <\head> part and insert the text before that.

On similar lines, the text below goes towards the end. The problem is
similar to the above one and I think all that needs to be done, is
read the text below from a file, do a similar search for some text
below which this insert part should go and put it there.

<p><script type="text/javascript">var mailSubject = 'Hardware
var mailBody    = 'Your  Technical Support Team hopes this topic will
be helpful: ' + location.href;
var mailDisplay = 'Click here to email this topic.';
    '<a href="'
    + '?subject=' + escape(mailSubject)
    + '&body=' + escape(mailBody)
    + '">' + mailDisplay + '</a>'

<script type="text/javascript"
    beginSearch ();

Any ideas would be helpful. I do not know much about perl and hence
the more the better.
Thanks a lot

Re: inserting text into HTML

Scripsit Slain:

Quoted text here. Click to load it

Oh, you mean </head>. Are you sure? Well, then you could write a Perl
few-liner to do the job, or ask someone to write it for you. You would still
need to learn how to run a Perl program _on the server_. The program could
be something like the following (untested):

undef $/;
while(<*.html>) {
  $filename = $_;
      || die "Can't open file \"$filename\":\n   $!";
  $stuff = <FILE>;
  $stuff =~ s?</head>?<script language=JavaScript
      || die "Can't open file \"$filename\" for writing:\n   $!";
  print FILE $stuff;

Of course, this isn't real about authoring in HTML. The group
would have been more suitable. And of course you might find an editor with a
built-in multi-file string replace feature that could be used for the job.

Quoted text here. Click to load it

Well, of course it would be similar, with similar solutions, though
detecting the end is non-trivial. But...

Quoted text here. Click to load it

This sounds like a _very_ clueless method of giving some users some poor way
of contacting you, instead of simply telling all users your email address.
So if you do something _so_ stupid, at least do it via some server-side
include feature, so that when you come to your senses, you won't have to fix
a zillion files again but can simply fix the included file.

Jukka K. Korpela ("Yucca")

Re: inserting text into HTML

Slain wrote :
Quoted text here. Click to load it

First language is deprecated; type is the normal replacement and it is
both backward and forward-compatible.

Quoted text here. Click to load it

It's </head>.
Just use an advanced text editor to do that. It's simple to do.

Free software (libre/open-source) text editors:

Quoted text here. Click to load it

You're overreaching here in my opinion. And your code has problems.
First, you must escape the ampersand with &amp;
Second, you are adding a closing </p> tag when there is not a start <p>.
Third, you'll need to escape the closing tags inside the document.write

Common HTML Validation Problems
Writing HTML in a SCRIPT element

HTML 4, Section 18.2.4 Dynamic modification of documents

HTML 4, Appendix B.3.2 Specifying non-HTML data, Element Content
"When script or style data is the content of an element (SCRIPT and
STYLE), the data begins immediately after the element start tag and ends
at the first ETAGO ("</") delimiter followed by a name start character
([a-zA-Z]); note that this may not be the element's end tag. Authors
should therefore escape "</" within the content."

Quoted text here. Click to load it

As mentioned already, language attribute is deprecated.

Don't use HTML comments as they are unneeded, unnecessary and
potentially harmful.

Don't Use HTML Comments In Script Blocks

Quoted text here. Click to load it

Validate your markup code! Preferably with/for a strict DTD. /

Using Web Standards in your Web Pages (Updated Apr. 2007)

Site Timeline