Solved

Simplify jquery

Posted on 2012-12-26
12
143 Views
Last Modified: 2012-12-27
Hello experts and merry christmas
I need helpto simplify the attached code:
f.e
I 'm using this code:
<br /><center><img src="' + staticurl + 'img/errorsmall.gif" /><br /><h4>' + defaulterror + '</h4></center>
OR:
p.html('<br /><center><img src="' + staticurl + 'img/errorsmall.gif" /><br /><h4>' + defaulterror + '</h4></center>');
OR:
 p.dialog("option", "buttons", {
                    End: function () {
                        p.empty();
                        $(this).dialog("close");
                        $(this).dialog("empty");
                    }
                })
more than once in the same function and in other functions in the same file.
// JavaScript Document
var p = $("#popuploader");
var staticurl = $('#msg_staticurl').attr('title');
var defaulterror = $('#msg_defaulterror').attr('title');
var globalprice = function () {
    p.dialog({
        autoOpen: false,
        bgiframe: true,
        title: "test",
        width: 420,
        height: 300
    });
    p.dialog("open");
    var d = $("#formitems");
    var c = d.serialize();
    p.html('<br><br><center><img src="' + staticurl + 'img/chef.gif" border=0 /></center>');
    $.post("myactionpage.cfm", c, function (g, ts) {
        if (ts == 'success') {
            if (g) {
                if ($.trim(g) == 'true') {


                } else {
                    p.html('<br /><center><img src="' + staticurl + 'img/errorsmall.gif" /><br /><h4>' + defaulterror + '</h4></center>');
                    p.dialog("option", "buttons", {
                        End: function () {
                            p.empty();
                            $(this).dialog("close");
                            $(this).dialog("empty");
                        }
                    })
                }

            } else {
                p.html('<br /><center><img src="' + staticurl + 'img/errorsmall.gif" /><br /><h4>' + defaulterror + '</h4></center>');
                p.dialog("option", "buttons", {
                    End: function () {
                        p.empty();
                        $(this).dialog("close");
                        $(this).dialog("empty");
                    }
                })
            }
        } else {
            p.html('<br /><center><img src="' + staticurl + 'img/errorsmall.gif" /><br /><h4>' + defaulterror + '</h4></center>');
            p.dialog("option", "buttons", {
                End: function () {
                    p.empty();
                    $(this).dialog("close");
                    $(this).dialog("empty");
                }
            })
        }
    })
};

Open in new window

0
Comment
Question by:Panos
  • 7
  • 5
12 Comments
 
LVL 7

Expert Comment

by:Element1910
ID: 38722416
Try this (haven't tested it, but it looks right and should work for you)...if not, then post error if possible.

Only part I'm unsure about is the dialogVar that I created. Might have the double- and single-quotes off.

// JavaScript Document
var p = $("#popuploader");
var staticurl = $('#msg_staticurl').attr('title');
var defaulterror = $('#msg_defaulterror').attr('title');
var htmlVar = "<br /><center><img src=" + staticurl + "img/errorsmall.gif /><br /><h4>" + defaulterror + "</h4></center>";
var dialogVar = "option, buttons, {" +
                        "End: function () {" +
                            "p.empty();" +
                            "$(this).dialog('close');" +
                            "$(this).dialog('empty');" +
                        "}" +
                    "}";
var globalprice = function () {
    p.dialog({
        autoOpen: false,
        bgiframe: true,
        title: "test",
        width: 420,
        height: 300
    });
    p.dialog("open");
    var d = $("#formitems");
    var c = d.serialize();
    p.html('<br><br><center><img src="' + staticurl + 'img/chef.gif" border=0 /></center>');
    $.post("myactionpage.cfm", c, function (g, ts) {
        if (ts == 'success') {
            if (g) {
                if ($.trim(g) == 'true') {


                } else {
                    p.html(htmlVar);
                    p.dialog(dialogVar)
                }

            } else {
                p.html(htmlVar);
                p.dialog(dialogVar)
            }
        } else {
            p.html(htmlVar);
            p.dialog(dialogVar)
        }
    })
};

Open in new window

0
 
LVL 2

Author Comment

by:Panos
ID: 38722978
Hi Element1910
The dialogVar is not working.
I willtry tomake some tests with the double- and single-quotes
0
 
LVL 7

Expert Comment

by:Element1910
ID: 38723852
Yeah, figured that it might not work with the quotes where they are. My apologies. When I get to work this morning, I'll try to help figure out that portion of it :)
0
 
LVL 2

Author Comment

by:Panos
ID: 38723870
OK. Thank you
0
 
LVL 7

Expert Comment

by:Element1910
ID: 38723875
Just thought of something....try this:

// JavaScript Document
var p = $("#popuploader");
var staticurl = $('#msg_staticurl').attr('title');
var defaulterror = $('#msg_defaulterror').attr('title');
var htmlVar = "<br /><center><img src=" + staticurl + "img/errorsmall.gif /><br /><h4>" + defaulterror + "</h4></center>";
var dialgVar = tempfunction () {p.dialog("option", "buttons", {
                    End: function () {
                        p.empty();
                        $(this).dialog("close");
                        $(this).dialog("empty");
                    }
                });};
var globalprice = function () {
    p.dialog({
        autoOpen: false,
        bgiframe: true,
        title: "test",
        width: 420,
        height: 300
    });
    p.dialog("open");
    var d = $("#formitems");
    var c = d.serialize();
    p.html('<br><br><center><img src="' + staticurl + 'img/chef.gif" border=0 /></center>');
    $.post("myactionpage.cfm", c, function (g, ts) {
        if (ts == 'success') {
            if (g) {
                if ($.trim(g) == 'true') {


                } else {
                    p.html(htmlVar);
                    p.dialog(dialogVar)
                }

            } else {
                p.html(htmlVar);
                p.dialog(dialogVar)
            }
        } else {
            p.html(htmlVar);
            p.dialog(dialogVar)
        }
    })
};

Open in new window

0
 
LVL 7

Expert Comment

by:Element1910
ID: 38723893
Wait...errr...scratch that...try this:

// JavaScript Document
var p = $("#popuploader");
var staticurl = $('#msg_staticurl').attr('title');
var defaulterror = $('#msg_defaulterror').attr('title');
var htmlVar = "<br /><center><img src=" + staticurl + "img/errorsmall.gif /><br /><h4>" + defaulterror + "</h4></center>";
var dialgVar = tempfunction () {p.dialog("option", "buttons", {
                    End: function () {
                        p.empty();
                        $(this).dialog("close");
                        $(this).dialog("empty");
                    }
                });};
var globalprice = function () {
    p.dialog({
        autoOpen: false,
        bgiframe: true,
        title: "test",
        width: 420,
        height: 300
    });
    p.dialog("open");
    var d = $("#formitems");
    var c = d.serialize();
    p.html('<br><br><center><img src="' + staticurl + 'img/chef.gif" border=0 /></center>');
    $.post("myactionpage.cfm", c, function (g, ts) {
        if (ts == 'success') {
            if (g) {
                if ($.trim(g) == 'true') {


                } else {
                    p.html(htmlVar);
                    dialgVar();
                }

            } else {
                p.html(htmlVar);
                dialgVar();
            }
        } else {
            p.html(htmlVar);
            dialgVar();
        }
    })
};

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 7

Accepted Solution

by:
Element1910 earned 500 total points
ID: 38723907
If that doesn't work...try this:

// JavaScript Document
var p = $("#popuploader");
var staticurl = $('#msg_staticurl').attr('title');
var defaulterror = $('#msg_defaulterror').attr('title');
var htmlVar = "<br /><center><img src=" + staticurl + "img/errorsmall.gif /><br /><h4>" + defaulterror + "</h4></center>";
var dialgVar = function () {p.dialog("option", "buttons", {
                    End: function () {
                        p.empty();
                        $(this).dialog("close");
                        $(this).dialog("empty");
                    }
                });};
var globalprice = function () {
    p.dialog({
        autoOpen: false,
        bgiframe: true,
        title: "test",
        width: 420,
        height: 300
    });
    p.dialog("open");
    var d = $("#formitems");
    var c = d.serialize();
    p.html('<br><br><center><img src="' + staticurl + 'img/chef.gif" border=0 /></center>');
    $.post("myactionpage.cfm", c, function (g, ts) {
        if (ts == 'success') {
            if (g) {
                if ($.trim(g) == 'true') {


                } else {
                    p.html(htmlVar);
                    dialgVar();
                }

            } else {
                p.html(htmlVar);
                dialgVar();
            }
        } else {
            p.html(htmlVar);
            dialgVar();
        }
    })
};

Open in new window

0
 
LVL 2

Author Comment

by:Panos
ID: 38723909
I'm getting a red line here: var dialgVar = tempfunction () {p.dialog("option", "buttons", {
0
 
LVL 7

Expert Comment

by:Element1910
ID: 38723914
try that last one I posted...meant to just use "function()" instead of "tempfunction()"
0
 
LVL 2

Author Comment

by:Panos
ID: 38723923
Lost your last post.
It is working .
Thank you very much
0
 
LVL 7

Expert Comment

by:Element1910
ID: 38723928
You're welcome :)
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 38723930
Thank you again.
regards
panos
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now