I need to run a JavaScript script when a document library is opened/shown. The script should be available only on the specific document library.

One possible solution could be adding the script directly in the site master page, but that would make it present on every page using that master page (even if the script isn't actually run - I could check the page url or something similar) while I would prefer to not have it included when unnecessary.

Can anyone provide some guidance on how to achieve this? I don't have specific requirements other than the above, so fell free to suggest the most fitting solution you may think of.

  • what is your purpose of adding javascript to doc library? Are you trying to do something with the way the docs list is displayed? OR are you trying to add javascript to all docs within the doc library?
    – variable
    Commented Feb 24, 2014 at 10:08
  • If you want JavaScript on your forms you could hack them in by adding a content editor web part directly to the form (they are web part pages). Just open a newform or editform then click Edit. This is a bit fragile and odd, but it works if you just want to "throw something in there". You question is a bit vague to know though
    – eirikb
    Commented Feb 24, 2014 at 13:42
  • can you elaborate some more on your actual needs? The javascript you want to add should be added to a specific document lib? doc lib type???
    – SPArcheon
    Commented Feb 24, 2014 at 15:27

2 Answers 2


You can create a new site page. Then, on that page add script editor webpart with for javascript code. On same page add your document library webpart (list view web part)


You can do as Nachiket wrote in his answer, or you can open page edit mode in your library and put script editor webpart directly to that library page :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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