Accessing dom in Server side scripting


(Sang) #1

Matrix Version:5.5.1.3

Hi,

I try to access Dom from server side script below

var links = document.getElementsByTagName('a');
var arr = [].slice.call(links);
if(arr.length > 1){
    arr.forEach(function(link){
        var href = link.href;
        if(href.endsWith("pdf") || href.endsWith("PDF")){
            var classes = link.className;
            if((!classes.indexOf('btnLink') !== -1) && (!classes.indexOf('btnLink') !== -1) && !link.getElementsByTagName('img')[0]){

                var pdfUrl = href;
                pdfUrl = pdfUrl.substring(8);
                var res = pdfUrl.split("/");
                var pdf = res[5];

                var fileSize = "%globals_asset_file_size_readable:"+pdf+"%";
                var el = document.createElement("span");
                el.innerHTML = " [PDF]";

                link.parentNode.insertBefore(el, link.nextSibling);
            }
        }
    });
}

However this does not work at all. If I try this in normal js, it works

Also I tried to execute keywords in normal js file like

var pdfUrl = res[5];
var pdf = "%globals_asset_file_size_readable:"+pdfUrl+"%";

But when I print out pdf, it does not show file size, shows %globals_asset_file_size_readable:1234%

Is there any way to implement this?


(John gill) #2

DOM access is impossible with SSJS. When you execute a script block with the runat="server" it only has access to whatever it inside the script block. I suggest using the ?SQ_VIEW_SERVER_JS query parameter to view the code that is executed by the JS engine, it’s helpful to get a feel for what is and isn’t possible. (https://matrix.squiz.net/manuals/concepts/chapters/server-side-javascript#viewing-ssjs)

The keyword you’re attempting to print out doesn’t work because keywords are evaluated before SSJS is executed.

https://matrix.squiz.net/manuals/concepts/chapters/server-side-javascript#processing-order

You can add the evalkeywords="post" parameter to the script tag to enable processing of globals_ keywords after the SSJS has run.


(Sang) #3

Is there any way server-side-javascript and normal javascript can communicate?


(John gill) #4

No.

SSJS is much simpler than it might seem at first glance. It really is just an extra step in the page rendering pipeline that can execute simple self-contained JS. It’s very useful within those bounds, but many people (everyone I’ve ever seen try to use it) find that those bounds are more limited than they initially hope.


(Douglas (@finnatic at @waikato)) #5

Server to client (normal) javascript comms should be doable by storing values in the page markup that are then later available to client side JS. Some level of comms between them might be possible to achieve that way and through the client side then calling on assets containing server side code, but I’ve not played in that space - we tend to have a firm divide between server generated content and then client code to augment whatever the server code has put in place.