SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

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

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi! Thanks so much! But I'm still having problem with it.
    Doesn't seem to work for me.

    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>

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    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
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! I'm getting what you mean.

    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

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

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Head on back to post #2 and re-read the second paragraph.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •