By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,280 Members | 2,263 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,280 IT Pros & Developers. It's quick & easy.

inserting text into HTML

P: n/a
I have a big list of HTML files, which need to be updated with a
common text.
<script language=JavaScript
src="./highlight.js"></script>

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 "<\headto it, might be one way to do it or
just find the <\headpart 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
Overview';
var mailBody = 'Your Technical Support Team hopes this topic will
be helpful: ' + location.href;
var mailDisplay = 'Click here to email this topic.';
document.write(
'<a href="mailto:Yo******@YourAddress.com'
+ '?subject=' + escape(mailSubject)
+ '&body=' + escape(mailBody)
+ '">' + mailDisplay + '</a>'
);</script></p>

<script type="text/javascript"
language=JavaScript1.2><!--
beginSearch ();
//--></script>
Any ideas would be helpful. I do not know much about perl and hence
the more the better.
Thanks a lot

May 18 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Scripsit Slain:
I need to add the above line in each of the html files, before the
text "<\head>". All the HTML files have this text
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 = $_;
open(FILE,"<$filename")
|| die "Can't open file \"$filename\":\n $!";
$stuff = <FILE>;
$stuff =~ s?</head>?<script language=JavaScript
src="./highlight.js"></script></head>?;
close(FILE);
open(FILE,">$filename")
|| die "Can't open file \"$filename\" for writing:\n $!";
print FILE $stuff;
close(FILE);
}

Of course, this isn't real about authoring in HTML. The c.i.w.a.tools 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.
On similar lines, the text below goes towards the end.
Well, of course it would be similar, with similar solutions, though
detecting the end is non-trivial. But...
var mailDisplay = 'Click here to email this topic.';
document.write(
'<a href="mailto:Yo******@YourAddress.com'
+ '?subject=' + escape(mailSubject)
+ '&body=' + escape(mailBody)
+ '">' + mailDisplay + '</a>'
);</script></p>
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")
http://www.cs.tut.fi/~jkorpela/

May 18 '07 #2

P: n/a
Slain wrote :
I have a big list of HTML files, which need to be updated with a
common text.
<script language=JavaScript
src="./highlight.js"></script>
First language is deprecated; type is the normal replacement and it is
both backward and forward-compatible.
I need to add the above line in each of the html files, before the
text "<\head>".
It's </head>.
Just use an advanced text editor to do that. It's simple to do.

Free software (libre/open-source) text editors:
http://en.wikipedia.org/wiki/List_of...open-source.29
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 "<\headto it, might be one way to do it or
just find the <\headpart 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
Overview';
var mailBody = 'Your Technical Support Team hopes this topic will
be helpful: ' + location.href;
var mailDisplay = 'Click here to email this topic.';
document.write(
'<a href="mailto:Yo******@YourAddress.com'
+ '?subject=' + escape(mailSubject)
+ '&body=' + escape(mailBody)
+ '">' + mailDisplay + '</a>'
);</script></p>
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 </ptag when there is not a start <p>.
Third, you'll need to escape the closing tags inside the document.write
block:

Common HTML Validation Problems
Writing HTML in a SCRIPT element
http://www.htmlhelp.com/tools/valida...html.en#script

HTML 4, Section 18.2.4 Dynamic modification of documents
http://www.w3.org/TR/html4/interact/....html#h-18.2.4

HTML 4, Appendix B.3.2 Specifying non-HTML data, Element Content
http://www.w3.org/TR/html4/appendix/...html#h-B.3.2.1
"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."

<script type="text/javascript"
language=JavaScript1.2><!--
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
http://www.javascripttoolbox.com/bes...ices/#comments

beginSearch ();
//--></script>

>

Any ideas would be helpful.

Validate your markup code! Preferably with/for a strict DTD.
http://validator.w3.org/

Gérard
--
Using Web Standards in your Web Pages (Updated Apr. 2007)
http://developer.mozilla.org/en/docs...your_Web_Pages
May 18 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.