0

I uploaded a JS script in Style Library. In the script a need to get a div by ID (div from the navigation) and add an button.

var div = document.getElementById('DeltaTopNavigation');
div.innerHTML += '<button>Button</button>';

Where i have to put <script type="text/javascript" src="script.js"></script> to show the button on every page.

1
  • 1
    As you need to show this button in every page, you can give this script reference in master page Commented Sep 5, 2016 at 9:08

2 Answers 2

0

As you need to show this button in every page, you can give this script reference in master page.

For .html master page, it will be

<!--SPM:<SharePoint:ScriptLink language="javascript" name="~sitecollection/{Library Path}/script.js"  runat="server" Localizable="false"/>-->

For .master

<script type="text/javascript" src="/{library path}/script.js"></script>
5
  • I tried to put it in seattle.master (on the begining and the end), but it displays me only in the new and edit forms, not the display forms and site pages. On some other pages it displays me but as soon the whole page is loaded it disapears. Commented Sep 5, 2016 at 9:29
  • It could be css issue or try executing your code inside SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() { // your code here }); Commented Sep 5, 2016 at 9:36
  • where can i find this? i am a newbie in sharepoint :S Commented Sep 5, 2016 at 9:46
  • update your script.js by SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() { var div = document.getElementById('DeltaTopNavigation'); div.innerHTML += '<button>Button</button>';}); Commented Sep 5, 2016 at 9:50
  • nothing has changed, i updated my script, added '<script type="text/javascript" src="/{library path}/script.js"></script>' on the end of the body of seattle.master, and the button stays only on some pages :( Commented Sep 5, 2016 at 9:56
0

Have you debugged your code? Is the div present when its executed?

From another answer of mine https://sharepoint.stackexchange.com/a/187867/10271

Here's a small utility to attach properly to the SharePoint lifecycle.

You use it like this:

window.LifeCycleHelper.AttachToLoad(function(){ alert('Hello World'); }); 

Utility:

//use an IIFE to create a scope and dont dirty the global scope
(function (_) {
    // use strict to ensure we dont code stupid
    'use strict';

    var initHandlers = [];
    var initMDSHandlers = [];

    var ensureSharePoint = function (handler) {
        var sodLoaded = typeof (_v_dictSod) !== 'undefined' && _v_dictSod['sp.js'] != null && _v_dictSod['sp.js'].state === Sods.loaded;

        if (sodLoaded) {
            handler();
        } else {
            SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { });
            SP.SOD.executeOrDelayUntilScriptLoaded(handler, 'sp.js');
        }
    };

    var initMDS = function () {
        for (var i = 0; i < initMDSHandlers.length; i++) {
            initMDSHandlers[i]();
        }
    };

    var init = function () {
        // Register MDS handler
        if ('undefined' != typeof g_MinimalDownload && g_MinimalDownload && (window.location.pathname.toLowerCase()).endsWith('/_layouts/15/start.aspx') && 'undefined' != typeof asyncDeltaManager) {
            asyncDeltaManager.add_endRequest(initMDS);
        } else {
            for (var i = 0; i < initHandlers.length; i++) {
                initHandlers[i]();
            }
        }
    };

    var registerInit = function (handler) {
        initHandlers.push(handler);
    };

    var registerInitMDS = function (handler) {
        initMDSHandlers.push(handler);
    };

    var domReady = (function (handler) {
        var fns = [];
        var listener;
        var loaded = (document.documentElement.doScroll ? /^loaded|^c/ : /^loaded|^i|^c/).test(document.readyState);

        if (!loaded) {
            document.addEventListener('DOMContentLoaded', listener = function () {
                document.removeEventListener('DOMContentLoaded', listener);
                loaded = 1;
                while (listener = fns.shift()) listener();
            });
        }

        return function (fn) {
            loaded ? setTimeout(fn, 0) : fns.push(fn);
        };
    })();


    var attachToLoad = function (functionToAttach) {
        registerInit(functionToAttach);
        registerInitMDS(functionToAttach);
       domReady(function () {
           init();
        });
    };

    _.AttachToLoad = attachToLoad;

    // THIS WILL PROTECT YOUR GLOBAL VAR FROM THE GARBAGE COLLECTOR
    window.LifeCycleHelper = _;
    if (window.Function != 'undefined' && typeof (Function.registerNamespace) == 'function') {
        Function.registerNamespace('LifeCycleHelper');
    }
})({});

var theCodeYouWantToRun = function () {
    alert('theCodeYouWantToRun');
};

window.LifeCycleHelper.AttachToLoad(theCodeYouWantToRun); 

More Information on how to work with lifecycle and MDS

Your Answer

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

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