Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript best practices - Using server-side values

For the past few years I have always used a client-side hidden <input> field to store a server-side value and use it in Javascript land.

For example, let's say I need an Ajax timeout value from my app configuration.

I'd probably store it like this in my JSP:

<input type="hidden" id="ajaxTimeout" value="${serverValue}" />

and then use it like this where my AJAX call lived in an external file:

$("#ajaxTimeout").val()

I was having a discussion about this today and it was suggested that it is best practice to store values which are only going to be used by Javascript within HTML <meta> tags.
Does this matter? Is there a preferred way to obtain server-side information which is solely to be used in Javascript?
My understanding is that if the hidden input field is not part of a form then it is safe enough to use to store value as it won't be attached to any requests. Having said that, I've always thought this was indeed a bit of a hack.

Thoughts?

::EDIT::
Two fantastic answers:

  • Use objects literals for general in-page data that is not tied to any particular DOM element.
  • Use data attributes to store custom data tied to DOM elements: http://www.w3.org/TR/2010/WD-html5-20101019/elements.html#attr-data
like image 606
wild_nothing Avatar asked Dec 11 '13 03:12

wild_nothing


People also ask

Is JavaScript good for server-side?

JavaScript on the server is not only possible but also recommended in most cases. It's fast, scalable, reduces development costs while increasing the speed and code quality. Every member of your team can work on all parts of the project and implement features from start to end.

Does JavaScript run server-side or client-side?

JavaScript. JavaScript is a client-side script, meaning the browser processes the code instead of the web server. Client-side scripts are commonly used when we want to validate data before sending it to the web server, adjusting the interface in response to user feedback, and for implementing other advanced features.

Should I use global variables in js?

The primary reason why global variables are discouraged in javascript is because, in javascript all code share a single global namespace, also javascript has implied global variables ie. variables which are not explicitly declared in local scope are automatically added to global namespace.


1 Answers

In addition to the plain old object literal method given in other answers, if the value you want to pass to the client is about a specific DOM element (or there is a DOM element that represents the logical object that the value is about), you can put the value in a data attribute:

<div id="videoplayer" data-startplayingat="1:02">HTML Content</div>

This is accessible as an entire attribute, data-startplayingat, or in modern browsers there is the dataset attribute. jQuery syntax is $('#videoplayer').data('startplayingat').

The official W3C spec on data attributes explains all this.

Here are a few interesting highlights:

  • The name must not use upper case letters, and must be XML compatible.
  • The dataset attribute converts dashes, such that a name like start-playing will become startPlaying.

One potential drawback for the object literal method (which I like and have used myself) is that if you want the object in a .js file, then normally static javascript files have to be run through your dynamic parser--which will cause a potentially small (but still present) performance loss. Putting the object declaration into a <script> tag in an HTML file works around this, but then you can have script load order issues to deal with.

like image 131
ErikE Avatar answered Sep 18 '22 15:09

ErikE