BuddyDev

Search

[Resolved] Very Basic JavaScipt Problem

  • Participant
    Level: Enlightened
    Posts: 66
    ljmac on #5407

    Hi Brajesh,

    Sorry to keep asking you for help, but you’re the best man I know to come up with simple, correct and timely answers for this stuff, and this one should be pretty simple. I’m trying to take a single JavaScript function and replicate it with some slight changes, so that both functions can run alongside each other. The problem is though that no matter what I do, only the second function works. How can I make both of them work? Here’s the original code:

    /*jslint regexp: true, undef: true, sloppy: true, eqeq: true, vars: true, white: true, plusplus: true, maxerr: 50, indent: 4 */
    var gdbbPressTools = {
        storage: { },
        get_selection: function() {
            var t = '';
    
            if (window.getSelection){
                t = window.getSelection();
            } else if (document.getSelection){
                t = document.getSelection();
            } else if (document.selection){
                t = document.selection.createRange().text;
            }
    
            return jQuery.trim(t.toString());
        },
        init: function() {
            jQuery(document).on("click", ".d4p-bbt-quote-link", function(e){
                e.preventDefault();
    
                if (jQuery("#bbp_reply_content").length > 0) {
                    var qout = gdbbPressTools.get_selection();
                    var id = jQuery(this).attr("href").substr(1);
                    var quote_id = '#d4p-bbp-quote-' + id;
    
                    if (qout === "") {
                        qout = jQuery(quote_id).html();
                    }
    
                    qout = qout.replace(/ /g, " ");
                    qout = qout.replace(/<p>|<br>/g, "");
                    qout = qout.replace(/<\/\s*p>/g, "\n");
    
                    if (gdbbPressToolsInit.quote_method === "bbcode") {
                        qout = "[quote quote=" + id + "]" + qout + "[/quote]";
                    } else {
                        var title = '<strong><a href="' + jQuery(this).attr("bbp-url") + '">';
                        title+= jQuery(this).attr("bbp-author") + ' ' + gdbbPressToolsInit.quote_wrote + ':</a></strong>';
                        qout = '<blockquote>' + title + qout + '</blockquote>';
                    }
    
                    if (gdbbPressToolsInit.wp_editor == 1 && !jQuery("#bbp_reply_content").is(":visible")) {
                        if (gdbbPressToolsInit.wp_version > 38) {
                            tinymce.get("bbp_reply_content").execCommand("mceInsertContent", false, qout);
                        } else {
                            tinyMCE.execInstanceCommand("bbp_reply_content", "mceInsertContent", false, qout);
                        }
                    } else {
                        var txtr = jQuery("#bbp_reply_content");
                        var cntn = txtr.val();
    
                        if (jQuery.trim(cntn) != '') {
                            qout = "\n\n" + qout;
                        }
    
                        txtr.val(cntn + qout);
                    }
    
                    var old_ie = jQuery.browser.msie && parseInt(jQuery.browser.version) < 9;
    
                    if (!old_ie) {
                        jQuery("html, body").animate({scrollTop: jQuery("#new-post").offset().top}, 1000);
                    } else {
                        document.location.href = "#new-post";
                    }
                }
            });
        }
    };
    
    jQuery(document).ready(function() {
        gdbbPressTools.init();
    });

    And here’s my modification (once again, only the second init: function works):

    /*jslint regexp: true, undef: true, sloppy: true, eqeq: true, vars: true, white: true, plusplus: true, maxerr: 50, indent: 4 */
    var gdbbPressTools = {
        storage: { },
        get_selection: function() {
            var t = '';
    
            if (window.getSelection){
                t = window.getSelection();
            } else if (document.getSelection){
                t = document.getSelection();
            } else if (document.selection){
                t = document.selection.createRange().text;
            }
    
            return jQuery.trim(t.toString());
        },
        init: function quote() {
            jQuery(document).on("click", ".d4p-bbt-quote-link", function(e){
                e.preventDefault();
    
                if (jQuery("#bbp_reply_content").length > 0) {
                    var qout = gdbbPressTools.get_selection();
                    var id = jQuery(this).attr("href").substr(1);
                    var quote_id = '#d4p-bbp-quote-' + id;
    
                    if (qout === "") {
                        qout = jQuery(quote_id).html();
                    }
    
                    qout = qout.replace(/&nbsp;/g, " ");
                    qout = qout.replace(/<p>|<br>/g, "");
                    qout = qout.replace(/<\/\s*p>/g, "\n");
    
                    if (gdbbPressToolsInit.quote_method === "bbcode") {
                        qout = "[quote quote=" + id + "]" + qout + "[/quote]";
                    } else {
                        var title = '<strong><a href="' + jQuery(this).attr("bbp-url") + '">';
                        title+= jQuery(this).attr("bbp-author") + ' ' + gdbbPressToolsInit.quote_wrote + ':</a></strong>';
                        qout = '<blockquote>' + title + qout + '</blockquote>';
                    }
    
                    if (gdbbPressToolsInit.wp_editor == 1 && !jQuery("#bbp_reply_content").is(":visible")) {
                        if (gdbbPressToolsInit.wp_version > 38) {
                            tinymce.get("bbp_reply_content").execCommand("mceInsertContent", false, qout);
                        } else {
                            tinyMCE.execInstanceCommand("bbp_reply_content", "mceInsertContent", false, qout);
                        }
                    } else {
                        var txtr = jQuery("#bbp_reply_content");
                        var cntn = txtr.val();
    
                        if (jQuery.trim(cntn) != '') {
                            qout = "\n\n" + qout;
                        }
    
                        txtr.val(cntn + qout);
                    }
    
                    var old_ie = jQuery.browser.msie && parseInt(jQuery.browser.version) < 9;
    
                    if (!old_ie) {
                        jQuery("html, body").animate({scrollTop: jQuery("#new-post").offset().top}, 1000);
                    } else {
                        document.location.href = "#new-post";
                    }
                }
            });
        },
        init: function reply() {
            jQuery(document).on("click", ".d4p-bbt-reply-link", function(e){
                e.preventDefault();
    
                if (jQuery("#bbp_reply_content").length > 0) {
                    var rply = gdbbPressTools.get_selection();
                    var id = jQuery(this).attr("href").substr(1);
                    var quote_id = '#d4p-bbp-quote-' + id;
    
                    if (rply === "") {
                        rply = jQuery(quote_id).html();
                    }
    
                    if (gdbbPressToolsInit.quote_method === "bbcode") {
                        rply = "[quote=" + id + "]";
                    } else {
                        var title = '<strong><a href="' + jQuery(this).attr("bbp-url") + '">';
                        title+= jQuery(this).attr("bbp-author") + '</a></strong>,';
                        rply = title;
                    }
    
                    if (gdbbPressToolsInit.wp_editor == 1 && !jQuery("#bbp_reply_content").is(":visible")) {
                        if (gdbbPressToolsInit.wp_version > 38) {
                            tinymce.get("bbp_reply_content").execCommand("mceInsertContent", false, rply);
                        } else {
                            tinyMCE.execInstanceCommand("bbp_reply_content", "mceInsertContent", false, rply);
                        }
                    } else {
                        var txtr = jQuery("#bbp_reply_content");
                        var cntn = txtr.val();
    
                        if (jQuery.trim(cntn) != '') {
                            rply = "\n\n" + rply;
                        }
    
                        txtr.val(cntn + rply);
                    }
    
                    var old_ie = jQuery.browser.msie && parseInt(jQuery.browser.version) < 9;
    
                    if (!old_ie) {
                        jQuery("html, body").animate({scrollTop: jQuery("#new-post").offset().top}, 1000);
                    } else {
                        document.location.href = "#new-post";
                    }
                }
            });
        }
    };
    
    jQuery(document).ready(function() {
        gdbbPressTools.init();
    });

    What an I doing wrong?

  • Participant
    Level: Enlightened
    Posts: 66
    ljmac on #5408

    I should mention that even if I swap the functions around, it’s always only the second one that works. So both functions work by themselves, they just don’t both work at the same time.

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #5439

    Hi Lee,
    You are over writing the functions. Trying using initB in the name for one and then you can call gdbbPressTools.initB();

    PS: Two member functions can not have same name.They will override the previous one.

    Hope that helps.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 66
    ljmac on #5440

    Hi Brajesh,

    I suspected it would be something like that, but I didn’t know the proper syntax for renaming the init: function. Thankyou so much for your help again.

    Regards,
    Lee

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #5534

    No problem. I am glad I could help.

    Regards
    Brajesh

The topic ‘ [Resolved] Very Basic JavaScipt Problem’ is closed to new replies.

This topic is: resolved