logo

NJP

Access Server Side variable directly in the tag of HTML in widget.

Import · Feb 07, 2020 · article

All,

Recently I came across as post on community where, the requirement was to access the server script variable in the section in the HTML code.</p> <p>As data.var_name or c.data.var_name are not accessible in the <script>, they throw <strong>data</strong> and <strong>c</strong> not defined error.</p> <p>It was obvious that these we not actually known to the scope of the HTML code.</p> <p>Now reading the AngularJS docs somewhere I came across a variable called &#39;<strong>top&#39;</strong> that is accessible in the script section in the HTML as it the $window kind of object.</p> <p>I made a following tweak to make this work.</p> <p>Hope this helps everyone, also some easy way to do this is much appreciated.</p> <p>HTML - </p> <div> <script> console.log(top.**Val**);

Client Controller -

function($scope) {

/* widget controller */

var c = this;

top.Val = c.data.valuefromserver;

}

Server Script -

(function() {

data.valuefromserver = "test";

})();

Here, as at the Server Side, we won’t be able to access the top variable as it is a window object, I had to do manipulations at the client side and push the data needed in the top object in order to access it in the section of HTML.</p> <p>Suggestions are always appreciated<img src="https://community.servicenow.com/community/s/html/@C05D81E748EA65D834D5446501E0DFAB/emoticons/1f60a.png" alt="image"></p> <p><u><strong>If this article helped you in any way, please then bookmark it or mark it as helpful.</strong></u></p> <p><strong>Regards</strong></p> <p><strong>Omkar Mone</strong></p> <p><strong>2019 Community Leader.</strong></p> <p><strong>2020 Community MVP.</strong></p>

View original source

https://www.servicenow.com/community/now-platform-blog/access-server-side-variable-directly-in-the-lt-script-gt-tag-of/ba-p/2290659