Solved

Simplify jquery

Posted on 2012-12-26
12
147 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Function after success of Jquery/AJAX action 9 42
JQuery Detecting page post back 6 19
AJAx/JQuery/JSON Error 3 9
sort Multi-dimensional array 6 10
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…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
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)

740 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