Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I was looking for help last week to get a simple javascript code made for SharePoint 2007 working for SharePoint 2010 and didn't really get a clear answer that i could use where i work unfortunately so i decided to try to just make my own. It's suppossed to be an Auction List and have a countdown in the "Time Left" field till the item expires, but i can't figure out what's wrong. I am very unfamiliar with javascript and sharepoint but i am an experienced programmer. Can anyone help with this? Here's the code below:

<script type="text/javascript">
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var lists = web.get_lists();
var listId = SP.ListOperation.Selection.getSelectedList();
var list = lists.getById(listId);
var item = getItemByName("End Date");
var end = Date.parse(item.text())/1000;
var todayNow = new Date();
todayNow = Date.parse(today)/1000;
var result = (end-todayNow);
var item2 = getItemByName("Time Left");
item2.text(result);
</script>

The End Date is a field that will probably be hidden, but just used as a placeholder to find the difference from now till the item expires.

Thanks guys for any responses.

share|improve this question
1  
One suggestion that I have, but not necessarily related to your issue, is use ExecuteOrDelayUntilScriptLoaded( function(){...}, "SP.js"); also, could you provide any error output from a debug console (F12 in IE)? –  Robert Kaucher Aug 22 '12 at 17:40
add comment

2 Answers

This may not be the root cause, but in your code snippet you did not include the </script> tag. Also, if you are using any other JavaScript libraries (I'm not sure if you are, I'm still a bit green) be sure to include them in the header.

You can close the code correctly in the following manner:

<script type="text/javascript"> 
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var lists = web.get_lists();
    var listId = SP.ListOperation.Selection.getSelectedList();
    var list = lists.getById(listId);
    var item = getItemByName("End Date");
    var end = Date.parse(item.text())/1000;
    var todayNow = new Date();
    todayNow = Date.parse(today)/1000;
    var result = (end-todayNow);
    var item2 = getItemByName("Time Left");
    item2.text(result);  
</script>

As a sidenote, to import any additional libraries simply lead your <script type="text/javascript"> with additional library calls. They look like this:

<script src="http://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript"></script>

Simply place one of those in front of your script body for each library you want to call.

share|improve this answer
2  
As a note about using the CDN: encosia.com/… –  Robert Kaucher Aug 22 '12 at 17:43
    
Didn't see closing script tags when I viewed the response! Sorry if the response wasn't the correct answer. –  Shrout1 Aug 22 '12 at 17:43
    
@Robert Kaucher Wasn't aware of that regarding the CDN. Thank you for the info. Was using the google API as an example but I'll change it up. Thanks! –  Shrout1 Aug 22 '12 at 17:45
    
A lot of people are not aware of it. I had not considered it until I read that article. I figure the more people who know, the better! –  Robert Kaucher Aug 22 '12 at 18:00
add comment

One useful technique for debugging javascript is to utilize the javascript console (F12 in IE, then click the 'Console' tab option). This is a live javascript REPL environment that you can use to run your statements one-by-one in the context of the current page. After each statement, you can inspect the variable(s) values so you better understand what each line is accomplishing.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.