Skip to main content
1 of 2

How to call javascript function on Quick Launch Navigation Item click?

I am working with SharePoint Online.

I have a requirement where I have to add Quick Launch Navigation Item and call JavaScript function on click of the item. I am using code shown here.

Following is my code:

var oQuickLaunchColl;
function addNavigation() {
    var testt = "javascript:callNavigationAction()";
    var clientContext = new SP.ClientContext();
    var oWeb = clientContext.get_web();
    oQuickLaunchColl = oWeb.get_navigation().get_quickLaunch();
    var oNavigation = new SP.NavigationNodeCreationInformation();
    oNavigation.set_title("Start new Chat");
    oNavigation.set_url(testt);
    oQuickLaunchColl.add(oNavigation);
    clientContext.load(oQuickLaunchColl);
    clientContext.executeQueryAsync(QuerySuccess1, QueryFailure1);
}
function QuerySuccess1() {
    //Get the item count of navigation nodes and loop through it  
    var itemCount = oQuickLaunchColl.get_count();
    console.log("The navigation node details are:");
    for (var i = 0; i < itemCount; i++) {
        var title = oQuickLaunchColl.get_item(i).get_title();
        console.log(title);
    }
}
function QueryFailure1(sender, args) {
    console.log('Request failed' + args.get_message());
}

Here I got error,

The URL format was invalid.

Note: I have also added item in Edit Control block of list item, also new action in Site Action menu, there I have used following code to call JavaScript function,

set_url('javascript:callNavigationAction()')

Here, my JavaScript function executed.

But when I tried the same approach for calling JavaScript function from Quick Launch Navigation it failed.

How can I call JavaScript function from Quick Launch Navigation item?