Adding Javascript To An Office 365 Sharepoint 2013 Public Web Page

101613_2057_addingjavas1Adding javascript to a 2013 public site page isn’t as straightforward as it is in most properly designed content management systems. The choices presented by the page editor are the “embed code” tool, the “edit html source” tool and buried away under web parts – the script editor web part. As far as I can tell here’s how all 3 work:The Embed Code Tool:This is the strangest of the Microsoft script insertion concoctions – it detects if your insertion starts with a at the start of your code, but it makes more sense to avoid it and just use the script editor web part to begin with.
The Edit HTML Source Tool:
Detects script code and strips it out – so not much use at all in this context.
The Script Editor Web Part:
The best and only logically designed built in option because it accepts ANY code. Find it under web parts » media and content.
This is really a confusing way of doing things because the "embed code" tool isn't rationally designed and is a prominent button in the ribbon while the script editor web part is buried in the list of available web parts with no clear indication it's there. A better design would be to eliminate the redundant "embed code" tool and just have the script editor web part (which lets you add ANY code) available in the ribbon. There's no need for both.
The "Roll Your Own" Option For More Advanced Users
Another possibly more convenient way to add javascript, is to put the javascript in a text file saved to an anonymously accessible library and then insert it into the page with a content editor web part. Doing it this way, you can edit the code directly using Windows Explorer or Sharepoint Designer and not have to open the clunky page editor. This method also makes it easier to reuse scripts you want to deploy on multiple pages, like Adsense ads or other script widgets.
1/. Paste your javascript into notepad - with script tags, so it looks like this example:

2/. Save as a .txt file and upload to the Site Assets library. Or even easier, create the file directly in Sharepoint Designer.
3/. Insert a content editor web part on your desired page. In the "content link" box, enter the url of your text file.


5/. Save/publish the page and now your javascript should run.