1

I am working on a SP2013 on-premise farm and try to create a custom page layout. There are some JQuery function I need it render on particular custom page layout but not other pages.

<script type="text/javascript">//<![CDATA[  
        hangCurrentURLBreadcrumb("path-breadcrumb-prefix");
//]]>
</script>

I added above snippet to my custom page layout html. It doesn't renderat all. However when I added it to the same location of my custom Master page, it works.

May I know what is the suggested way to add JS or JQuery to custom layout page? Thanks.

2

In your custom page layout first add path to .js file (put path to src="") and then code.

<script src="jquery-1.8.3.min.js">//<![CDATA[
//]]>
</script>

<script>//<![CDATA[

            $(document).ready(function(){
            //some code         
            });

//]]>
</script>
<!--MS:<Publishing:EditModePanel runat="server" id="editmodestyles">-->
...

Place it just above the Publishing:EditModePanel tag

  • Oh! It works now. The only change I made is moving the snippet above Publishing:EditModePanel tag. Why it makes the different? – Mark L Jul 2 '15 at 8:11
  • It must be before all that publishing registration for jquery to work. Please mark as answer. :) – Marko Tica Jul 2 '15 at 8:24
1

You can also add your scripts in the following section too.

 <SharePoint:DelegateControl runat="server" ControlId="AdditionalPageHead" 
      AllowMultipleControls="true"/>
1

FYI,

If you are only after using (simple) jQuery selectors.

Microsoft has this thing called mQuery built in SharePoint.

So jQuerys:

$('#myID') 

becomes:

m$('myID')

straight out of the box, without the need for jQuery

It is loaded on most pages (check in F12 developer tools)
if not, use:

EnsureScriptFunc('mQuery.js', 'm$', function() {
// DO STUFF
}

see

Note that with not too complex JavaScript; jQuery "magic" can be done without jQuery;
http://youmightnotneedjquery.com/ is a very good read

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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