Solved

Simplify jquery

Posted on 2012-12-26
12
145 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
The viewer will learn how to dynamically set the form action using jQuery.
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…

770 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