Javascript & HTML in Sharepoint "Content Editor Web Part"

I’m trying to put in my javascript codes and html codes into the sharepoint “content editor web part” source editor.

i have this set of javascript codes that needs to work together with the html codes.

i created a link using ahref with the tagname as “homepage”. i want to hide this “homepage” link using javascript codes.

this is the codes that I’m using right now. the html part is working fine[the link “Homepage” is being display and is able to click on] but not for the javascript part. Please help and advise. I’m a newbie in Javascript. Thanks. :slight_smile:

<html>
<head>
<script language=“Javascript” type=“text/Javascript”>
var d = new Date();
if (d.getHours() < 12 || d.getHours() >= 14) {
document.getElementsByTagName(‘Homepage’)[0].style.display = ‘none’;
}
</script>
</head>

<p><a href=“my url link”>Homepage</a></p>
</html>

The language attribute needs to be removed, and type should be text/javascript (all lowercase) but that’s not the issue causing you trouble.

The problem is that when the script runs, it cannot see anything that is below it. Move the script to just before the </body> tag and you will be right.

Hey! You have no body tag!

The <body>…</body> tags need to wrap the content after the end of the head, and before the end html tag.

Hi! Thanks so much! But I’m still having problem with it.
Doesn’t seem to work for me. :confused:

have made the changes according to your advice. Am i right to place the “<a href>” after the </head> tag? Or is there any problem with my script using the “document.getElementsByTagName(‘Homepage’)” to get the tagname which is homepage out to hide it??
please help…Thanks!

<html>
<head>
<body>
<script language=“Javascript” type=“text/javascript”>
var d = new Date();
if (d.getHours() < 12 || d.getHours() >= 14) {
document.getElementsByTagName(‘Homepage’)[0].style.display = ‘none’;
}
</script>
</body>
</head>

<p><a href=“my url link”>Homepage</a></p>
</html>

After the </head> is where you put the <body>…</body> tags. They wrap the content of the web page until just before before the </html> tag.

See: Introduction to the structure of an HTML document

Thanks! I’m getting what you mean. :slight_smile:

But my script still doesn’t seems to work.
I’m trying “(d.getHours() < 12 || d.getHours() >= 14)” right now.
Logically the link should not be display now but i’m still seeing the link. currently i only have one link so i suppose it’s right that i’ve input “[0]” to show that the link i want to hide is the first link :confused:

<html>
<head>
<script language=“Javascript” type=“text/Javascript”>
var d = new Date();
if (d.getHours() < 12 || d.getHours() >= 14) {
document.getElementsByTagName(‘Homepage’)[0].style.display = ‘none’;
}
</script>
</head>
<body>
<p><a href=“my url link”>Homepage</a></p>
</body>
</html>

Head on back to post #2 and re-read the second paragraph.

I think you are using the wrong function. document.getElementsByTagName is looking to find a tag named “homepage”, i.e. <homepage>, but what you have is a tag called <p> and a tag called <a>. It isn’t finding what you are sending it after because it doesn’t exist.

Now, if you used jQuery (which is also javascript, but modularized), you could use the “contains (text)” function, where “text” is replaced with “homepage”, and that would do what you are looking for, it would look for something that had “homepage” in it. I do not think there is a document.getElementsByText function in javascript. You could use document.getElementsByID, and then give that <p> an id name.

As an aside, when I put javascript into a content editor web part, I don’t put <html><head><body> etc tags. I would think they would be more confusing to have them in there, because the web part is being built into the existing page, and that webpart will likely be below where the <head> has already completed.

What was important at the time was helping him to get the scripting working, so that later on we can go in to correctly getting page elements.

That’s good advice though.