Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to hide Web Part between certain dates-SharePoint 2010

Status
Not open for further replies.

cutum

Technical User
Sep 25, 2007
8
US
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>
 
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

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
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
 
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 "[highlight #FCE94F]function hidepart1[/highlight] "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 [highlight #FCE94F]function hidepart1[/highlight] 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);

f[highlight #FCE94F]unction hidepart1()[/highlight] {
document.getElementById("MSOZoneCell_WebPartWPQ2").style.display = "none";} }

</script>

<a id="myLink" onclick="hidepart1();return false;" href="#">Click here to hide calendar</a>
 
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
 
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
 
Yes, here's the ID [highlight #FCE94F]<td id="MSOZoneCell_WebPartWPQ2"[/highlight]

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

[COLOR=#a6a6a6]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";} }[/color]

[b][COLOR=#A40000]hidepart1();[/color][/b]
</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
 
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!
 
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() {
[b]alert("The element is: " . document.getElementById("MSOZoneCell_WebPartWPQ2"));[/b]
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
 
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!
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top