KenEucker KenEucker - 1 year ago 167
Javascript Question

#ifndef in javascript

I am looking for a solution to only define a function once in Javascript using something exactly like #ifndef in compiled languages. I found a couple of libraries that were supposed to mimic this functionality but they didn't work.

I am working with MVC 3 Razor and have defined some html helpers do put what are essentially user controls onto the page.

Each control has a set of javascript functions that define specific functionality for that control, so herein lies the issue: the functions get defined multiple times when the helper is called multiple times on a single page.

I am hoping to find a way to keep the very small amount of javascript defined within the helper and not have to divide all of the javascript for each of these small helpers in a separate file.


@helper CmsImage(int id)
var Html = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Html;

<input type="button" class="editor_function" style="display: none;" onclick="editImage(@id); return false;" />
function editImage(id) {
$('#alt_text' + id).attr('value', $('#' + id).attr('alt'));
$('#image_url' + id).attr('value', $('#' + id).attr('src'));

function saveImage(button, id) {
$(button).parent().siblings('div.widget-content').html('<img alt="' + $('#alt_text' + id).val() + '" src="' + $('#image_url' + id).val() + '" id="' + id + '" />');
Image Url:
<input type="text" id="image_url@{id.ToString();}" /><br />
Alt Text:
<input type="text" id="alt_text@{id.ToString();}" /><br />
<input type="button" value="save" onclick="saveImage(this, @{id.ToString();});" />

The above doesn't work in the browser, if gives me the error: '48: Unrecognized token ILLEGAL'

Answer Source

As I presume you know, Javascript doesn't have preprocessor directives like C/C++, but you can use regular if statements that are evaluated at run-time like this:

if (typeof myFunc === "undefined") {
    var myFunc = function(a,b) {
        // body of your function here

or for a whole library of functions:

if (!window.controlUtilsDefined) {
    window.controlUtilsDefined = true;

    // put control library functions here

    function aaa() {
        // body here

    function bbb() {
        // body here


or if you want to check based on some other variable:

var myFunc;
if (debugMode) {
    myFunc = function(a,b) {
        // body of your function here
} else {
    myFunc = function(a,b) {
        // body of your alternate function here

If your concern is just that you have multiple copies of the exact same function names in the library that each control uses, that isn't technically a problem in Javascript. The last defined one will be the operative one, but if they're all the same that isn't technically a problem. Only one definition will exist in memory as the later definitions will replace the earlier ones.

If you control the source of the controls, then it would be better to break the common utilities out separately into their own JS file and have the host page just include that utilities script file once.

Or (with a little more work but no additional responsibilities for the host page), each control could dynamically load their utlities from an external JS file and check a known global variable to see if some other control has already loaded the common external JS.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download