简体   繁体   中英

How do I debug JavaScript in Sharepoint?

I just started working at a new place as software developer and they have put me on a project that needs to be improved as a way to get me started.

It's a sharepoint 2010 project and it's made up of C# and ASP. I have little experience at all in developing for sharepoint so I would like to ask for some assistance if possible. Basically they have a page with a lot of activies and each activity is supposed to try and save the company money.

Now, when you press "Edit item" a JavaScript executes and styles some of the table that is presented to you. Since this project was made in a rush there is no documentation and there are hard coded values. What I wanted to try first was to change the year numbers it displays as these are hard coded in. So I'd go from this:

var quarters = {
    "Q3": {
        title: "Q3",
        name: "Q3 (MAR-MAY)"
    },
    "Q4": {
        title: "Q4",
        name: "Q4 (JUN-AUG)"
    },
    "2015-16": {
        title: "2015-16",
        name: "Budget year 2015/2016"
    },
    "2016-17": {
        title: "2016-17",
        name: "Budget year 2016/2017"
    }
};

to this:

function getCurrentYear() {
    var year = new Date().getFullYear();
    return year;
}

var quarters = {
    "Q3": {
        title: "Q3",
        name: "Q3 (MAR-MAY)"
    },
    "Q4": {
        title: "Q4",
        name: "Q4 (JUN-AUG)"
    },
    "CurrentYear-NextYear": {
        title: getCurrentYear + "-" + (getCurrentYear()+1).toString().substr(2,2),
        name: "Budget year" + getCurrentYear() + "/" + getCurrentYear()+1
    },
    "NextYear-YearAfterNextYear": {
        title: (getCurrentYear() + 1) + "-" + (getCurrentYear() + 2).toString().substr(2, 2),
        name: "Budget year " + (getCurrentYear() + 1) + "-" + (getCurrentYear() + 2)
    }
};

But when I deploy the sharepoint site and use the "Edit Items" part, nothing that the JaveScript is supposed to do happens. I tried to debug but the debugger never actually enters the function that I try to use which leads me to believe that the website never uses the JavaScript in the first place.

My question is: Where would I check for a possible use of the script?

I searched throughout the entire solution and nowhere is the JavaScript file referenced but it works fine on production and the Revision History reveals nothing that would break the usage of the JavaScript.

throughout the entire solution and nowhere is the JavaScript file referenced

since you mentioned that this was done in a rush maybe the script reference was added in the aspx after the page was deployed? (this is an extremely dirty way to do things but not the worst i've seen in a sharepoint context)

to check this: on the server, look for the aspx at the deployment location (usually something like: C:\\Program Files\\Common Files\\microsoft shared\\Web Server Extensions\\14\\TEMPLATE\\LAYOUTS\\<ProjectName>\\ ) and see if the script file is referenced in the application page there.

it could also be that another script file on the page has been modified in the hive which is loading the target script file: i'd check the script files in the hive for calls like SP.SOD.execute , executeFunc and EnsureScriptFunc or just SP.SOD .(besides looking for the file name obviously)

also: the script might be added via a different solution(if there are any) as delegate control so that might be another thing to check if you can get the source for that.

To debug JavaScript, hit F12 to bring up the browser's developer tools, including the JavaScript console. Then you generally have to refresh the page to get the code to run again.

You'll only be able to see errors generated by code that actually gets executed, so if you don't see any errors, your code may not be running.

You can also use the JavaScript console to type in part or all of your code manually to see what works and what doesn't work.

I noted in a comment that there seems to be a missing set of parentheses after one of your calls to getCurrentYear but without the full code I can't guarantee that's the only problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM