Hdiv compliant url setting through Javascript, ajax

353 Views Asked by At

I have inherited legacy code that invokes a Javascript in external file for a link action. Below is a snippet of the JS function

function webaction(){
$.ajax({
    url:contextpath + '/docheck.html'
    // more logic below

As can be seen, since is not used, it is failing HDIV validation. How should I create the URL inside the JS for HDIV compliance? Is it possible to define JSTL tag inside the JS function so that I can use

<c:url>

inside the JS?

1

There are 1 best solutions below

1
On

You have an explanation with examples of the integration of HDIV with AJAX on this webpage.

Summarising, you have to create all link and form elements on the server side, if they are created on the client side HDIV does not have any way to process them.

Here it is an example with pure JS:

<body>

    <h1>AJAX Example</h1>
    <c:url value="/ajax/ajaxTime.html" var="url1" />
    <h2><div id="myDiv" data="${url1}">Let AJAX make this call</div></h2>
    <button type="button" onclick="loadXMLDoc()">View data time</button>
    
</body>

<script>
    function loadXMLDoc() {
        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
            }
        }
         
        xmlhttp.open("GET", document.getElementById("myDiv").getAttribute("data"), true);
        xmlhttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
        xmlhttp.send();
    }
</script>

I hope it helps

Fernando Lozano (HDIV Team)