473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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
3 2667
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.