0

I'm not sure if this is a relevant programming question for this site, or if it belongs elsewhere, but I'll ask anyways.

Currently I am working with a javascript file that has a structure that looks like this, I came up with in on my own and was super proud when I did

$(function(){
    var dir = document.URL;

    if(dir.indexOf('contact') > -1){
         // do stuff in here for contact/index.php...
    }else if(dir.indexOf('about') > -1){
         // do stuff in here for about/index.php...
    }else{
         // everything for the index page goes here
   }
});

It is a very elegant and beautiful code set up and I love it. However, I build large admin dashboards for a living, and I have found that the file can get very extensive, like 3000+ plus lines long. While I know this is not an unusual thing it is a literal pain in the butt to figure out where some things are some days. Is there some kind of include function for javascript that would help me organize this?

other issues involving this structure

It has been brought to my attention that there is an error in my logic structure and now I have work around that too.

It was pointed out that what happens when my URL looks like the following:

http://...index.php?contact=1

Then logically I would be running the wrong code, how do I get around this?

research results

So far I've found some things where they say you can do a $.get call on another js file, but that seems highly inefficient to me.

Does document.write do anything for me here?

Mark Hill
  • 1,769
  • 2
  • 18
  • 33
  • Not an answer to your question, but a flippant scenario: what happens when your site grows to include something like `/about/index.php?tab=contact` - with this logic you'll be running the wrong code. – James Thorpe Oct 08 '15 at 15:15
  • yeah I've considered that, and while I have not run into any instances, you have a point – Mark Hill Oct 08 '15 at 15:16

0 Answers0