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

is javascript capable of reading the source of the page it is being loaded in?

P: n/a
Hi all,

I have a page which contains the following tag:

<head>
<meta name="date_modified" content="20030730" />
</head>
(...)
<script>
(...)
</script>

I would like the javascript to print the date written in the meta tag. Is it
possible and how do I do that?

It should work with any browser (Netscape or IE, any version supporting
javascript).

Thanks!

PS: I can print the title with "document.write(document.title)", but it
doesn't seem to work with the meta tag...
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Christian Caron wrote:
Hi all,

I have a page which contains the following tag:
The following is a snippet of HTML that contains a number of tags. Remember
that tags are simply the beginning and end of elements - elements are the
important bits.

<head>
<meta name="date_modified" content="20030730" />
</head>
(...)
<script>
Please validate your HTML before asking for help.

<URL:http://validator.w3.org/>

Invalid code can cause problems when you least expect it, and often people
will ignore requests for help when they spot invalid code.

<URL:http://diveintomark.org/archives/2003/05/05/why_we_wont_help_you>

(...)
</script>

I would like the javascript to print the date written in the meta tag. Is
it possible and how do I do that?
Yes, it's possible. You don't do it by reading the source of the page
though - you do it by traversing the representation of the document built
up by the browser (i.e. use the DOM).

For instance:

metas = document.getElementsByTagName('meta')
for (i = 0; i < metas.length; i++) {
if (metas[i].getAttribute('name') == 'date_modified') {
document.write(metas[i].getAttribute('content'))
}
}

It should work with any browser (Netscape or IE, any version supporting
javascript).
That's not "any browser", and in any case, both of those browsers allow the
user to turn off Javascript. Use the normal object detection methods and
provide a suitable fallback where appropriate. Not all browsers that
support Javascript support the DOM, but the code I posted above is fairly
well-supported, I believe.

PS: I can print the title with "document.write(document.title)", but it
doesn't seem to work with the meta tag...


You are confusing the DOM with elements here. The DOM provides a title
property for the HTMLDocument object you are accessing. You aren't
directly accessing the <title> element when you refer to 'document.title',
and you can't generally access elements in this way.
--
Jim Dabell

Jul 20 '05 #2

P: n/a

"Christian Caron" <no****@nospam.org> wrote in message
news:bn**********@nrn2.NRCan.gc.ca...
Hi all,

I have a page which contains the following tag:

<head>
<meta name="date_modified" content="20030730" />
</head>
(...)
<script>
(...)
</script>

I would like the javascript to print the date written in the meta tag. Is it possible and how do I do that?


For future references, I was able to do it like this:

var metaData = document.getElementsByTagName("meta");
for(var i = 0; i < metaData.length; i++) {
if (metaData[i].name == 'date_modified') {
dateModified = metaData[i].content;
}
}

I don't know if it's the best way, but it works in Netscape 7 and IE 6 (not
in Netscape 4.79).

I would prefer to find a solution that works in all browsers, so I'll
continue my searches.

Christian
Jul 20 '05 #3

P: n/a
I like Jim's idea ...

Now for the solution which is ugly ...

Create a file include_me.js

With

function write_header() {
s = "This is the header text"
return s;
}
Then on the html page have

<script src="include_me.js"></script>
<script>
write_header();
</script>

There are other ways of doing it ...
If you have apache and you have server side executes
enabled then you can use exec script tags
and call out a perl cgi

#!/usr/local/bin/perl
print<<EOT;
This is the header text";
EOT
1;

I forget the syntax for server side exec's, it's something like

<exec file="header.cgi">
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.