Solved

Simplify jquery

Posted on 2012-12-26
12
148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

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!

Question has a verified solution.

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

Suggested Solutions

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…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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)

710 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