user2739418
user2739418

Reputation: 1631

Sitefinity adding javascript to custom master page

I am new to Sitefinity. We are trying to create some custom master pages in which we need to include some javascript files. so we have created a project as below: enter image description here

In the Default.Master we are trying to add .js file as below. But it is not working

<%@ Master Language="C#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />


    <script src="../Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="~/Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>

</head>

When I view source of html page it adds the reference as below

 <script src="../jquery-1.9.1.min.js" type="text/javascript"></script>
 <script src="core.min.js" type="text/javascript"></script>
<script src="~/jquery-1.9.1.min.js" type="text/javascript"></script>

But none of that working.

I even put .js files in theme (Global folder) even that is not working.

Cheers

Upvotes: 0

Views: 2688

Answers (5)

Binh LE
Binh LE

Reputation: 377

With Sitefinity project, I think you need to follow the structure rule, below is the sample: Sitefinity Project Structure

After change the Structure, you can load Js or Css in master page like that: Ex 1: load Js

<script src="/Sitefinity/WebSiteTemplates/SunwayTemplate/JS/modernizr-2.7.1.min.js"></script>

Ex 2:load Css

<link href="/Sitefinity/WebSiteTemplates/SunwayTemplate/App_Themes/SunwayTheme/Global/style.css" rel="stylesheet">

You can read more information about the Sitefinity Structure in: http://docs.sitefinity.com/website-templates-file-structure

Upvotes: 2

Veselin Vasilev
Veselin Vasilev

Reputation: 3793

What I usually do is put the Master page under the root of the project and also I have a /js folder under the root as well.

So in the master page I simply refer to /js/file.js and it always works fine.

Never bother to use the "default" Sitefinity structure

Upvotes: 0

Steve Mallory
Steve Mallory

Reputation: 4283

I typically use the RadScriptManager.

<form id="aspnetForm" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" CompositeScript-ScriptMode="Release" EnableScriptGlobalization="True" EnableScriptLocalization="False" runat="server" CompositeScript-NotifyScriptLoaded="True" LoadScriptsBeforeUI="True" OutputCompression="Forced" AjaxFrameworkMode="Disabled">
        <CompositeScript>
            <Scripts>
                <asp:ScriptReference Name="Telerik.Sitefinity.Resources.Scripts.MicrosoftAjax.js" Assembly="Telerik.Sitefinity.Resources" />
                <asp:ScriptReference Name="Telerik.Sitefinity.Resources.Scripts.MicrosoftAjaxWebForms.js" Assembly="Telerik.Sitefinity.Resources" />
                <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" />
                <asp:ScriptReference Path="~/Scripts/Modernizr-2.6.2.min.js" />
                <asp:ScriptReference Name="Telerik.Sitefinity.Resources.Scripts.jquery-1.8.3.min.js" Assembly="Telerik.Sitefinity.Resources" />
                <asp:ScriptReference Name="Telerik.Sitefinity.Resources.Scripts.jquery-ui-1.9.2.custom.min.js" Assembly="Telerik.Sitefinity.Resources" />
                <asp:ScriptReference Path="~/Scripts/bootstrap.min.js" />
             </Scripts>
        </CompositeScript>
        <Scripts>
            <asp:ScriptReference Path="~/Scripts/project.js" />
        </Scripts>
    </telerik:RadScriptManager>

    ...

</form>

This loads everything in the correct order and I can easily add third party libraries to a mimimized file and my own scripts separately for easier debugging.

Upvotes: 1

Brian Scott
Brian Scott

Reputation: 9361

I had a very similiar problem recently. Despite getting the js files to include correctly the order of the script files was being reorganised.

I found a more reliable way to include my scripts was to add a code-behind file to my masterpage and specify the following;

 protected void Page_Load(object sender, EventArgs e)
 {
 InitPageScripts();
 }

Within my InitPageScripts method I have the following;

private void InitPageScripts()
{
//Get the reference for the RadScriptManager that is on the master page
var scriptManager = ScriptManager.GetCurrent(Page);
if (scriptManager == null) return;
PageManager.ConfigureScriptManager(Page, ScriptRef.JQuery);
scriptManager.Scripts.Add(new ScriptReference { Path = "~/js/myscript.js" });            
}

Upvotes: 0

Jon R.
Jon R.

Reputation: 999

<%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI" TagPrefix="sf" %> <sf:ResourceLinks ID="resourcesLinks" runat="server"> 
<sf:ResourceFile JavaScript Library="JQuery" /> 
 <sf:ResourceFile Name="~/Widgets/myJavascript.js" />
</sf:ResourceLinks>

http://www.sitefinity.com/documentation/documentationarticles/define-the-resourcelinks-widget

http://www.sitefinity.com/blogs/gabe-sumners-blog/2011/09/01/how_to_use_jquery_and_other_javascript_libraries_in_sitefinity

Upvotes: 2

Related Questions