简体   繁体   中英

Call external javascript file functions from SharePoint 2013 Script Editor

Teaching myself JavaScript (primarily for SharePoint 2013) and I'm having issues calling a function from an external file in the Script Editor web part. Here is what is working:

<img src="../SiteAssets/Main_Building_Third.jpg" usemap="#map">
<map name="map">
       <area shape="rect" coords="0,0,82,126" href="javascript:void(0);" onclick="doSomething();">
</map> 

<script language="javascript">
    function doSomething() {
        alert("Hello World");
    }
</script>

Here is what is not working:

<script src="../SiteAssets/HelloWorld.js" type="text/javascript"></script>        

<img src="../SiteAssets/Main_Building_Third.jpg" usemap="#map">
<map name="map">
       <area shape="rect" coords="0,0,82,126" href="javascript:void(0);" onclick="doSomething();">
</map> 
<script language="javascript">
    function doSomething() {
        doSomethingElse();
    }
</script>

In the not working example, I have a .js file in the Site Asset doc lib that contain the following:

<script language="javascript">
function doSomethingElse() {
   alert("Hello World");
}
</script>

The only difference I see between the two is that one is calling a function that is in an external .js file. I have searched lots of places, and everyone seems to say that calling functions in an external .js file is super easy. So I think the issue lies within the Script Editor web part. Please note that I'm using Script Editor (not Content Editor) which seems to be the new home for this type of thing in 2013.

Any help is greatly appreciated.

Do not include html in your js file. The file should only contain:

function doSomethingElse() {
  alert("Hello World");
}

If you were using a Content Editor Web Part, you could link it to an external txt file containing html.

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