1

A simple JavaScript alert for script editor is not working on clicking ok. I am using sharepoint office 365. enter image description here

3
  • Check your console for errors. If you are in Chrome, it will block this and you willl see an XSS auditor error. Apr 25, 2016 at 15:02
  • How to check console? I am new to sharepoint
    – Karen
    Apr 25, 2016 at 15:09
  • 1
    I mean the debugging console (developer tools). You can access this by pressing F12 in the Chrome browser. Please see my answer and follow the JS Link method instead. Apr 25, 2016 at 15:12

2 Answers 2

1

The script editor web part will not execute in Google Chrome thanks to Chrome's built-in XSS Auditor. This is a feature of Chrome that is designed to mitigate Cross-site Scripting (XSS) attacks.

There are ways to workaround this, but IMO a better solution is to just use JS Link. Upload the script file that you are wanting to run on the page, and then link it using JS Link. I usually place a scripts folder under Site Assets, and that keeps all my scripts in one place, making them easier to manage and update.

Edit: Providing steps for using JS Link.

Step 1: Upload script file(s).

I am using the built in Site Assets Library with a scripts folder that I have added. You can create a new separate library, if you prefer. We just need a place to store the script(s).

In your example, you need to paste the code into a file, make sure you save it with .js extension. Name it whatever you want, and then add it to the library of your choice.

enter image description here

Step 2: Link the script with JS Link.

Once you have the file added, we need to link it to your page. Navigate to the list/page you are wanting to add the script. Click the gear(settings icon) and click on Edit Page. Then select the main web part, and Edit Web Part. enter image description here

You now have an edit menu on the right side of the page. Scroll to the bottom and expand on the Miscellaneous tab, and see the option for JS Link.

enter image description here

You will paste your file link url in this column. You can paste the full url as sitename.sharepoint.com/siteassets/scripts/myscript.js OR you can use a shortcut if the folder is on the existing site like this : ~site/siteassets/scripts/myscripts.js. Note, if you are needing to add more than 1 script you can use the pipe character |. So, if had 2 scripts, my JS Link would look like this ~site/siteassets/scripts/myscript.js | ~site/siteassets/scripts/myscript2.js.

Save your changes and stop editing the page, and you now have successfully linked your scripts.

11
  • Is a Content Editor WebPart affected by this as well, Alex? Apr 25, 2016 at 15:21
  • I dont know what content editor webpart is :(
    – Karen
    Apr 25, 2016 at 15:24
  • I am trying to run this script for creating charts. simego.com/blog/2013/09/charts-and-office-365-sharepoint-online
    – Karen
    Apr 25, 2016 at 15:24
  • I just tested trying with CEWP and embedding the code, but I get the same XSS error. Apr 25, 2016 at 15:25
  • JS Link is a far better solution IMO, pasting scripts in CEWP is a "quick and dirty" fix. Tough to manage, update, reuse the script(s). Apr 25, 2016 at 15:26
0

Did you check the markup of the page after you saved everything? Sometimes it can get stripped out.

Save the page and look at it's source (Ctrl+U) and do a ctrl+f for your snippet.

Another thing to check is if you have the Minimal Download Strategy enabled. Do you have start.aspx in the URL? enter image description here

I found that it interfered with my scripts. You can test if that is the problem by visiting the page directly, so instead of:

  • {site}/_layouts/15/start.aspx#/SitePages/MyPage.aspx

you'd visit:

  • {site}/SitePages/MyPage.aspx

Edit Just reproduced this on my own 365 tenant. It is the Minimal Download Strategy that is preventing it from firing. I visited the page directly and my alert worked. enter image description here

1

Your Answer

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

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