Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I've recently been going through some of the code used through my site, one of them being a "News Slider" which shows some of the latest news articles. The code is using javascript at the moment, however I would like to know how the best way would be of converting it to jquery.

The code is as follows:

var PromoSlideShow = function () {
    var b = [];
    var f = 0;
    var e = false;
    var a = false;
    var d = function (g) {
        if (!e && !a && f != g) {
            a = true;
            Effect.Fade(b[f], {
                duration: 0.8,
                from: 1,
                to: 0
            });
            f = g;
            if (f >= b.length || f <= -1) {
                f = 0
            }
            Effect.Appear(b[f], {
                duration: 0.8,
                from: 0,
                to: 1,
                afterFinish: function () {
                    a = false
                }
            });
            c(f)
        }
    };
    var c = function (g) {
        $$("#promo-bullets a").each(function (h) {
            h.removeClassName("active")
        });
        $$("#promo-bullets a")[g].addClassName("active")
    };
    return {
        init: function () {
            b = $$("#promo-box .promo-container");
            if (b.length < 2) {
                return
            }
            if ($("promo-bullets")) {
                $("promo-bullets").insert('<a href="#" class="active">0</a>');
                for (var g = 1; g < b.length; g++) {
                    $("promo-bullets").insert('<a href="#">' + g + "</a>")
                }
                $$("#promo-bullets a").each(function (i) {
                    i.observe("click", function (j) {
                        Event.stop(j);
                        e = false;
                        d(parseInt(j.target.innerHTML, 10));
                        e = true
                    })
                })
            }
            var h = 6000;
            setInterval(function () {
                d(f + 1)
            }, h)
        }
    }
}();

Thanks in advance! :)

share|improve this question
 
Here is a reference for that: net.tutsplus.com/tutorials/javascript-ajax/… –  Kutluhan Metin Jul 8 '13 at 7:01
4  
This code is using prototype, not POJS. –  Florian Margaine Jul 8 '13 at 7:24
 
This question appears to be off-topic because it is about converting code to make use of a specific framework rather than reviewing code already written. –  codesparkle Jul 8 '13 at 11:04
 
Even if it was vanilla JavaScript, why would you want to convert it to jQuery? –  cimmanon Jul 12 '13 at 14:53
add comment

closed as off-topic by codesparkle Jul 8 '13 at 11:04

  • This question does not appear to be a code review request within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

Some hints.

  • jQuery is just a JavaScript library for dom manipulation. There is no such thing as 'porting jQuery code to JavaScript.

  • AVOID single letter variable names, they're unreadable and very annoying. Code readability is one of the most important factor in your code. Second only to correctness. Often more important than performance for example. You can't understand what's doing on with short un-meaningful variable names

  • Use semicolons consistently, don't drop them some places. Semicolon insertion can really bite you. See this question in SO on why.

  • You have HTML in strings, avoid those - they mix up your presentation/business logic. You want your web page to separate what you show and what state you have coherently and create nice structure.

  • Same goes for parsing innerHTML. You can keep state in JavaScript variables.

  • Prefer === to == and !== to !=, generally avoid stuff like !something unless you know something is a boolean. See this SO question on why.

Good luck! I hope these suggestions helped, feel free to ask any questions about these if you'd like further reference.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.