How to call an external Javascript file from a web part

I am creating a web part for SharePoint 2010, and I would like to add a simple modal .

I registered an external js script as follows:

ScriptLink.Register(this.Page, "js/jquery-1.5.min.js", true); ScriptLink.Register(this.Page, "js/jquery.simplemodal-1.4.1.js", true); 

Somehow I get this message that the file was not found, because it is looking for the 1033/_layouts or something like that.

So my question is: how can I link to an external JavaScript file from my website without putting them in this directory?

+4
source share
4 answers

In my opinion, you should expand your scripts into layouts, as well as images, style sheets, etc. that are not intended for users to customize.

You can map the Layouts folder to your project in VS 2010. Then add subfolders to display the name of your project, etc. (Right-click on the project → Add → SharePoint Layouts "Mapped Folder)

Layouts
- ProjectName
- - Scripts
- - - jquery-1.5.min.js

Then, when you deploy your solution, the scripts will be copied to the right place.

In your website, you can link to your scripts, for example:

In code:

 ScriptLink.Register(this.Page, "ProjectName/Scripts/jquery-1.5.min.js", false); 

But I prefer in .ascx:

 <SharePoint:ScriptLink ID="ScriptLink2" Name="ProjectName/Scripts/jquery-1.5.min.js" runat="server" OnDemand="false" Localizable="false" /> 
+11
source

If you set the "Localizable" attribute to "false" in the ScriptLink tag, it will omit the "1033" folder.

+2
source

I'm not sure if this still works in SharePoint 2010, but here is how I did it in SharePoint 2007:

 ScriptLink.Register(this.Page, "js/jquery-1.5.min.js", true); List<string> list = (List<string>)HttpContext.Current.Items["sp-ScriptLinkValues"]; int index = list.Count - 1; string item = list[index]; list[index] = item.Replace("/_layouts/1033/js", "http://ajax.googleapis.com/ajax/libs/jquery/1.5"); 
0
source

If you add another Content Editor web part, you can put your Include code in a JavaScript file with the full location of the file.

-1
source

Source: https://habr.com/ru/post/1340653/


All Articles