2

Need to add canonical tag in jsp

<link rel="canonical" url="http://www.example.com"/>

But i dont want to hard code the url value.

How can i achieve this.

I have tried using data binding techniques but in view source, link tag is shown as

<link rel="canonical" href="" data-bind="attr:{href: ko.toJS($root.seoURLCanonical)}"/>

instead of

<link rel="canonical" url="http://www.example.com"/>

while inspecting we can see canonical link tag as

<link rel="canonical" href="http://www.example.com" data-bind="attr:{href: ko.toJS($root.seoURLCanonical)}"/>

1 Answers1

1

You can set the id of the html tag and attach ViewModel to it:

Simple example:

<html id="htmlTag">
<head>
    <script src="<c:url value='/static/js/knockout-3.4.0.js' />"></script>
    <script src="<c:url value='/static/js/jquery-1.10.2.js' />"></script>

    <link rel="canonical" data-bind="attr:{url: seoURLCanonical}"/>
</head>
<body>
<script type="text/javascript">
    $(function () {
        var viewModel = {
            seoURLCanonical: "http://www.example.com"
        };

        ko.applyBindings(viewModel, document.getElementById("htmlTag"));
    });
</script>
</body>
</html>

In the DOM inspector you will see:

enter image description here