• Hi all,
    I have wordpress plugin with js-file attached for js functions.
    I want to escape possible error of having js functions with the same name as js functions pf other developers so I use some prefix like “nsnpas_” in functions name and inputs name.
    This prefix I define as php constant

    define( 'nsnPluginPrefix', 'nsnpas_' );
    
    and use it like :
    <script type="text/javascript" language="JavaScript">
        /*<![CDATA[*/
        ...
        var <?php echo nsnPluginPrefix ?>obj_productsAjaxSearchBox = new <?php echo nsnPluginPrefix ?>productsAjaxSearchBox(  { nsnPluginPrefix : 'nsnpas_', ...   },
            use_in_search_array, autocomplete_save_options_as_array );  // That is object's ref to my js functions
        ...
                <input value="<?php echo get_search_query() ?>" name="input_<?php echo nsnPluginPrefix ?>search" id="input_<?php echo nsnPluginPrefix ?>search" class=""  placeholder="Type search" />&nbsp;
       ...
    
                <img src="<?php echo $plugin_url ?>images/arrow-down.png" alt="Open Options" title="<?php echo esc_html__("Open Options") ?> style="cursor: pointer;"  onclick="javascript:<?php echo nsnPluginPrefix ?>obj_productsAjaxSearchBox.openOptions();">  <!-- margin-left: 10px; width: 16px;height: 16px; -->

    and in my js file ti which I set nsnPluginPrefix as:

    var this_m_nsnPluginPrefix;

    function nsnpas_productsAjaxSearchBox(Params, use_in_search_array, autocomplete_save_options_as_array) { // constructor of backend artist’s editor – set all params from server
    selfproductsAjaxSearchBox = this;
    this_m_nsnPluginPrefix = Params.nsnPluginPrefix;
    this_plugin_url = Params.plugin_url;

    }

    nsnpas_productsAjaxSearchBox.prototype.openOptions = function () {
    $( “#span_”+this_m_nsnPluginPrefix+”openOptions”).css(“display”,”none”)
    $( “#span_”+this_m_nsnPluginPrefix+”hideOptions”).css(“display”,”inline”)
    $( “#div_”+this_m_nsnPluginPrefix+”ajaxSearchOptions”).css(“display”,”inline”)
    }

    It works for me. but I have to write function’s object as literal “nsnpas_”. I mean If I need to change nsnPluginPrefix contant, I would have to change “nsnpas_” literal by hand in all js file.
    If there is a way to generate this prefix programmatically in js file or can this be salved i some different way?

    Thanks!

  • The topic ‘js-methods with prefix specified’ is closed to new replies.