Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I discoverd my jquery code is a mess, right now it doesnt follow any type of logic build up. I have lots of ajax calls, nested functions, dom manipulations, plugin calls in my main js file.

Lot of calls are fired on every page, what is a no-no, and get the undefined error on a few pages because some functions or calls fire on every page, and thats not supose to.

Example codes

var siteUrl = window.location.protocol+"//"+window.location.host + "/fresh/"
    uid = $('.profile-social').data('connection'), 
    sample = $('.profile-samples').data('sample');

// profile read more
$('div.profile-bio').expander({
    slicePoint: 670,
    expandText: '<i class="icon-chevron-down"></i> Read more',
    userCollapseText: '<i class="icon-chevron-up"></i> Hide Text'
});

// load profile sample works
$('.samples').socialist({
    networks: [
         {name:sample.name ,id: sample.tid}, 
       ],
    isotope:true,
    random:true,
    fields:['source','heading','text','date','image','followers','likes']
})


loadSocial();
// load useres followers and folowings
function loadSocial() {
    $.ajax({
      url: siteUrl+'user/connection/' + uid,
      type: 'get',
      dataType: 'html',
      success: function(data) {
        $(".social-list").html(data);
      },
    });
}

I know this is not a good way, and im not following any pattern to organise my code. Anybody can give me an advice? Because right now its a straight mess, and lot of events fire on every page, and i would like to avoid that

share|improve this question
This site is for reviewing working code. If you have problems with the code itself, head over to StackOverflow and fix them first. When it's working, put your code here for review. Besides, there is nothing much to optimize in your code. They are all just function calls. – Joseph the Dreamer Feb 1 at 0:19
@JosephtheDreamer they told me the same thing on stackowerflow, to come here... – Side Feb 1 at 8:43
1  
The thing is that you are asking for 1. what's causing the code to go awry - that we don't answer here. we can't answer it on StackOverflow. You just provided us a series of function calls. No procedure to debug. 2.) what can you optimize - And again. Your code is just a series of function calls. No procedure to optimize as well. – Joseph the Dreamer Feb 1 at 12:06

closed as off topic by Winston Ewert Feb 27 at 16:29

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

I would advise you to not have all your javascript in one main.js.

I find it best to have the main.js containing only the javascript that I need on all pages. Then I have page specific javascript in line on the page, or in a separate file that only gets loaded by the specific page / pages.

share|improve this answer

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