SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Aug 2002
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help Reduce Code

    Hi, this is my fisrst post!!

    Im mainly into webdesign, but am familiar with HTML/ASP, Ive created a site using Dreamweaver MX and part of the <HEAD> code looks like this :-

    <html>
    <head>
    <style type="text/css">
    body {
    margin-bottom: 0px;
    }
    .awstitle { font-family: "Times New Roman"; font-size: 24px; font-weight: bold}
    .awstext { font-family: "Times New Roman"; font-size: 16px; font-weight: 100}
    </style>
    <script language="JavaScript">
    <!--
    function MM_swapImgRestore() { //v3.0
    var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
    }

    function MM_preloadImages() { //v3.0
    var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
    }

    function MM_findObj(n, d) { //v4.0
    var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && document.getElementById) x=document.getElementById(n); return x;
    }

    function MM_swapImage() { //v3.0
    var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
    if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
    }
    //-->
    </script>

    </head>

    Could someone please help and advise me on how I can tidy up this code to improve speed, etc.

    1) Should the CSS be in a seperate file?
    2) Should Script be in a different file?

    If so, how is this achieved?

    Also because it was created in Dreameaver, to be honest i dont even know what the code is doing!

    thankyou

  2. #2
    .NET inside archigamer's Avatar
    Join Date
    Jan 2002
    Location
    Strongsville OH
    Posts
    1,534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    putting them in seperate files wont speed it up much but it will be much much easier to manage. the adavantage of putting them in a seperate file is that if you ever need to change the code you just have to update the file once and its all done so you dont have to go to all the other files and edit them.

    put your javascript code in a textfile with the extension .js to include it use
    Code:
    <script language="javascript" src="path/to/mysrc.js"></script>
    to include css put all the css definitions in a file with the extension of .css then in the head of your code put
    Code:
    <link rel="stylesheet" href="path/to/mycss.css" type="text/css">
    Web Finesse Studios
    Professional, business oriented web hosting and development.

  3. #3
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually putting them in separate files could slow things down (more separate server calls) but it is much tidier!

    Side point really.

    Quick DW tip. If you want to squash their code a little, find and replace all occurances of MM_ with nothing. Saves you 3 bytes every time a DW script refers to or creates a function or variable. It can add up!

    G

  4. #4
    SitePoint Member
    Join Date
    Aug 2002
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh, thanx - its easy when you know how??

  5. #5
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That DW generated script is bulky and inefficient.

    Someone re-write it for him before i do.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  6. #6
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  7. #7
    SitePoint Member
    Join Date
    Aug 2002
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - how can i improve on this bulky script? im only a beginner, i dont even know if it all needed!

  8. #8
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    findObj would work just as well as

    funciton de(obj){ return document.getElementById(obj); }

    That obviously cuts out some browsers - but i don't really tend to care about them.

    The swap image function is bulky - the preload function should do the variable declaration of objects and the swapimage funciton should just be one single line - would someone else like to outline this - kinda busy right now

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  9. #9
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Flawless_koder
    That DW generated script is bulky and inefficient.
    Originally posted by Flawless_koder
    findObj would work just as well as

    funciton de(obj){ return document.getElementById(obj); }

    That obviously cuts out some browsers - but i don't really tend to care about them.
    Actually, the code above is excellent piece of programming, and is designed to cope with every browser known to mankind (as long as it has JavaScript)!! Making unsubstantiated statements like your first (reproduced above) doesn't really tell anyone anything, other than your personal opinion. (Which you are of course entitled to)

    And in your second statement, just because you don't care about your code running in anything other than W3C DOM level 1 compliant browsers (i.e. it will only work for IE 5+ and Netscape 6+ !!!) doesn't mean that these issues are irrelevant to AndyT (or other people)!

    While greg.harvey's tip about replacing the "MM_" prefix may at first sound like a good plan, Macromedia added this prefix not only because they invested time and money writing the code (and thus want to leave their mark), but also because the JS "Behaviour" system that DreamWeaver & Fireworks both use relies upon the presence of the MM_ prefix to distinguish between its own JS functions and user written "Custom Script" code. Thus, although removing the prefix from the code may save a minute amount of space, it will actually stop you being able to edit assigned behaviours in DreamWeaver/Fireworkds, and is therefore best left alone! (Incidentally, placing it in a separate JS include file will also disrupt the "Behaviour" functionality.)


    IMHO there is no need to touch any of this code at all, since it is as cross-browser as it is possible to be, it is efficient, and is required by the behaviours you have applied (indeed, the code will be regenerated by DW/FW every time you modify the relevant behaviour anyway)! It also comes to less that 1K in size, which in terms of download time is utterly insignificant.

    I rest my case, m'lud!

    Flawless - if you are indeed able to come up with code which does everything that the DW code snippets above do and is somehow "better" in some measurable way as well, then please post it, or sell it to Macromedia so that we can all benefit!!!
    Last edited by M@rco; Aug 24, 2002 at 09:17.
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  10. #10
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just noticed that you mentioned CSS as well. This should indeed be moved to an external file, so that you only have to change that one file to effect a change to the formatting of the entire site - ease of administration is the major advantage here, not speed of download (although this will be marginally improved, but not significantly).
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  11. #11
    SitePoint Member
    Join Date
    Aug 2002
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, thanks for all your comments?

    I will put the CSS in another file?

    Is it possable to put the script further down the HTML page, as ive heard that search engines spider from the top down and that all content should be nearer the top of the page as possable for better SEO. Is this why its another good idea to put code in a seperate file?

  12. #12
    SitePoint Evangelist
    Join Date
    Jan 2002
    Posts
    420
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by AndyT
    Is this why its another good idea to put code in a seperate file?
    Yes it is more search engine friendly in just one line of code rather than a big chunk of it.
    Wavelan


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •