Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simplify jquery

Posted on 2012-12-26
12
Medium Priority
?
151 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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)
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

971 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