INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Need to hide Web Part between certain dates-SharePoint 2010

Need to hide Web Part between certain dates-SharePoint 2010

Need to hide Web Part between certain dates-SharePoint 2010

(OP)
I need to make a web part in(SharePoint 2010) visible to users during a certain date range. My attempt is to use Java to do so, however, the code doesn't seem to work. I know the function works as I've successfully implemented it using and "on click" function with a button. That said, I would like to hide it using Java. Please advise if another solution is more viable or if something is wrong with my JavaScript. Thanks

Here's my Java:

<!--DOCTYPE html--><script>
var d = new Date();
var n = d.getDate();
var p = d.getMonth();

if ((d.getMonth() >= 2 && <= 3))
function myFunction() {

$('#MSOZoneCell_WebPartWPQ2').hide();
} </script>

RE: Need to hide Web Part between certain dates-SharePoint 2010

Quote:

My attempt is to use Java to do so

This is the javascript forum, Java is a different thing entirely and is found at forum269: Java

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Need to hide Web Part between certain dates-SharePoint 2010

(OP)
Sorry, I meant JavaScript.

RE: Need to hide Web Part between certain dates-SharePoint 2010

What exactly do you want to hide? How is it not working? Is your what you want to hide inside an element with an ID of MSOZoneCell_WebPartWPQ2?



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Need to hide Web Part between certain dates-SharePoint 2010

(OP)
I am trying to hide a sharepoint calendar for most of the year. I only want it visible for a month or two. I'm attaching the code that I have been working on. The onclick button logic works fine and hides the calendar as I want, however, the "function hidepart1 "line after I display the popup after 3 seconds does not execute. I added the 3 second delay and popup just to see if it was making it down to that point. I do not know why the function hidepart1 will not execute.
I know I need to add additional code to my if statement to show the calendar when I need it.

<script>

var d = new Date();
var n = d.getDate();
var p = d.getMonth();

if (d.getMonth() >= 6 ) {

setTimeout(function() { alert(d.getMonth()); }, 3000);

function hidepart1() {
document.getElementById("MSOZoneCell_WebPartWPQ2").style.display = "none";} }

</script>

<a id="myLink" onclick="hidepart1();return false;" href="#">Click here to hide calendar</a>

RE: Need to hide Web Part between certain dates-SharePoint 2010

Quote:

I do not know why the function hidepart1 will not execute.
Scope is the reason


The function only 'exists' in the local scope of setTimeout().

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Need to hide Web Part between certain dates-SharePoint 2010

Nope. The function only exists when d.getMonth is greater than or equal to 6.
But other than that, hidepart1 is not within the scope of timeout.

The code works for me as is, so is will again ask, is there an HTML element with an ID of MSOZoneCell_WebPartWPQ2 somewhere?
Are you getting any JS errors? Most browsers have Js consoles that will let you check for this. What browser are you using to test this on?






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Need to hide Web Part between certain dates-SharePoint 2010

(OP)
Yes, here's the ID <td id="MSOZoneCell_WebPartWPQ2"

I am not getting any JS errors
I am running IE11- I have tried my code in Firefox and Chrome, both produce the same results as IE11.

I only get the popup box with a "6" in it(which represents July). It will not execute the code after that to hide the MSOZoneCell_WebPartWPQ2. However, when I click the button that I created the onclick logic, it will hide just fine.

Thank you very much for your help!!

RE: Need to hide Web Part between certain dates-SharePoint 2010

Ahh, I see now. The button you created executes your function. But you are not executing it anywhere else. You are only declaring it. You need to actually call it like in the button for it to be executed. i.e:

CODE

<script>

var d = new Date();
var n = d.getDate();
var p = d.getMonth();

if (d.getMonth() >= 6 ) {

setTimeout(function() { alert(d.getMonth()); }, 3000);

function hidepart1() {
document.getElementById("MSOZoneCell_WebPartWPQ2").style.display = "none";} }

hidepart1();
</script> 




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Need to hide Web Part between certain dates-SharePoint 2010

(OP)
Thank you for your reply! It makes sense, but it still will not work. I do not know what I am missing. The code is exactly as you have it. Any more thoughts? I appreciate it!

RE: Need to hide Web Part between certain dates-SharePoint 2010

2 things. Where is this code in relation to your HTML. Its possible that when this runs, the element in question has not yet loaded, so the getElementById cannot find it, thus, nothing is hidden.

Also, I mistakenly added the function call outside of the IF statement, so it will attempt to run it always whether it exists or not.

You can add an alert to your hidepart1 function to see if its getting run.

CODE

function hidepart1() {
alert("The element is: " . document.getElementById("MSOZoneCell_WebPartWPQ2"));
document.getElementById("MSOZoneCell_WebPartWPQ2").style.display = "none";} 

If the alert shows, you know its being run, the alert will also tell you whether the element exists or not. It should show something like: The element is: [object HTMLTableCellElement]

If it shows Null, or Undefined, your element does not exist when the function is being run.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Need to hide Web Part between certain dates-SharePoint 2010

(OP)
Hi Vacunita- First, many thanks for your help! It appears that my JavaScript was executing prior to ("MSOZoneCell_WebPartWPQ2")being loaded. I ended up moving the JavaScript to the bottom of the HTML page and it started working. Thank you for pointing that out to me!

Here's my final working code:

<script>
var d = new Date();
var n = d.getDate();
var p = d.getMonth();

function hidepart1() {
document.getElementById("MSOZoneCell_WebPartWPQ2").style.display = "none";}


if (d.getMonth() == 6 ) {
hidepart1();}

</script>

My last challenge is to get the element to hide (if between months 6 and 8). I'm hopeful that I can figure that one out. I am new to JS and have a lot to learn!

Thanks again!

RE: Need to hide Web Part between certain dates-SharePoint 2010

Glad I could help and Good Luck.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close