Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6562703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:44:26+00:00 2026-05-25T13:44:26+00:00

I am looking for a solution to only define a function once in Javascript

  • 0

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.

Sample:

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

  <text>
    <input type="button" class="editor_function" style="display: none;" onclick="editImage(@id); return false;" />
    <script>
        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().parent().removeClass('color-yellow').addClass('color-red');
            $(button).parent().siblings('div.widget-content').html('<img alt="' + $('#alt_text' + id).val() + '" src="' + $('#image_url' + id).val() + '" id="' + id + '" />');
        }
        #endif 
    </script>
    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();});" />
    @Html.Raw(GetCurrentContent(id))
  </text>
}

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:44:26+00:00Added an answer on May 25, 2026 at 1:44 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for an online solution for generating .ICO files. I'd like the ICO
I'm looking for a general solution for upgrading database schema with ORM tools, like
I am looking for a simple templating solution for ruby, i.e. something that will
Looking for a solution in bash (will be part of a larger script). Given
I was looking for a solution for client side printing of reports and pre
Just looking for the first step basic solution here that keeps the honest people
I'm looking for a quick-and-dirty solution to this, I have to set up a
I am looking for a better solution than what we currently have to deal
We are looking for a free solution to compress our SQL Server backups for
I'm looking for a good solution for graphically viewing a SOAP-based web service and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.