• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

What to do with JavaScript?


bigdave

Moderator
Staff member
#1
I'm putting Emmas website together and I'm not sure how I should be dealing with the required javascript? Obviously I wont be writing it myself, just reusing from various sources but if every time I collect some more javascript to do something else, I stick the code in the head of the page, it's going to get very confusing! Should I be gathering all of these bits into separate external .js files to be linked to pages as and when required, or all into one external .js file? If I use external .js will a link in the head suffice (I know there are bits that need to be in the body too but yeah, you get what I mean... lol)?
 
#2
you can either have them linked like you do for a .css file "<link rel="stylesheet" type="text/css" href="theme.css">" or if you dont wanna do that or cant for any reason then just add this /* name or description of java file */ so it will seperate them and you can edit when needed.
 
#3
I'm absolutely no coder but I think you reference the javacript something like:

<head>
<script type="text/javascript" src="myscript.js"></script>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>

But you need a Code Monkey here.
 
#5
Concatenate all of your scripts into one js file, add them as a script at the bottom of the page so you'd have all of the javascript code in one file. Why? To minimize HTTP requests and add them to the bottom just before the closing body tag so then the JS doesn't block the CSS being loaded.

Hope that helps, it makes your site a bit faster and is a pretty good practice to use.
 

bigdave

Moderator
Staff member
#7
Concatenate all of your scripts into one js file, add them as a script at the bottom of the page so you'd have all of the javascript code in one file. Why? To minimize HTTP requests and add them to the bottom just before the closing body tag so then the JS doesn't block the CSS being loaded.

Hope that helps, it makes your site a bit faster and is a pretty good practice to use.
So.... make a .js file and link it in the head with <script type="text/javascript" src="myscript.js"></script> then just before the </body> tag stack the <script></script> code required throughout the body of the page rather than distributing it after each attribute?
 
Last edited:
#8
Make a javascript file and add it in the bottom of the page. Example:

HTML:
...
</body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> <!--Get jQuery if needed-->
<script type="text/javascript" src="myscript.js"></script> <!--Your Scripts all in one file to minimise requests -->
</html>
Sometimes however...some scripts have to be added in the head if they are impacting the rendering of the page, for example if I have a jQuery plugin which modifies the layout it needs to be loaded before the layout gets rendered. As a general rule of thumb however scripts should be at the bottom.
 
#10
DUDE!!! STOP - Learn to write it your self or your end up being buggered !!! If you take lots of people scripts and throw them all together then if any errors come up you wont know how to fix them... if you need some help man hook me up! You need to do it your self from scratch - using Jquery ETC is fine but just throwing a load of stuff together and linking them together like a patchwork quilt will only bring you grief! Also bangin a load of stuff in the head tag is bad for SEO so consider that too...

if you need some help feel free to ask!

you can get my email off my main site - Website Designer

Also - check my sig for a pure Jquery interface project - its extensive (and encrypted) but written with Jquery from scratch.. DO IT YOUR SELF !!!!!! :dizzy: you will feel better and totally understand what you have done.
 
Last edited:

bigdave

Moderator
Staff member
#11
I know I 'should' learn javascript etc.. and it's on my list of things to do but at the moment I really don't have the time to learn something else. I know that reusing code isn't going to help me in the long run but if it works for now then that'll do me.
 

bigdave

Moderator
Staff member
#12
OOOOKkkkk..........

So the not knowing whats going on with .ks has just bitten me in the arse! when pasted into the head of my web page it runs fine BUT when I create an external file it stops working and Dreamweaver starts shouting at me about a syntax error on line 6!?! any idea why an inline script works fine but external gets cross?

The code in question is:

<SCRIPT>

var rollOverArr=new Array();
function setrollover(OverImgSrc,pageImageName)
{
if (! document.images)return; <--this is the line giving me problems
if (pageImageName == null)
pageImageName = document.images[document.images.length-1].name;
rollOverArr[pageImageName]=new Object;
rollOverArr[pageImageName].overImg = new Image;
rollOverArr[pageImageName].overImg.src=OverImgSrc;
}

function rollover(pageImageName)
{
if (! document.images)return;
if (! rollOverArr[pageImageName])return;
if (! rollOverArr[pageImageName].outImg)
{
rollOverArr[pageImageName].outImg = new Image;
rollOverArr[pageImageName].outImg.src = document.images[pageImageName].src;
}
document.images[pageImageName].src=rollOverArr[pageImageName].overImg.src;
}

function rollout(pageImageName)
{
if (! document.images)return;
if (! rollOverArr[pageImageName])return;
document.images[pageImageName].src=rollOverArr[pageImageName].outImg.src;
}
</SCRIPT>
 

bigdave

Moderator
Staff member
#13
the above should have read:

So the not knowing whats going on with .js has just bitten me in the arse! when pasted into the head of my web page it runs fine BUT when I create an external file it stops working and Dreamweaver starts shouting at me about a syntax error on line 6!?! any idea why an inline script works fine but external gets cross?

The code in question is:

<SCRIPT>

var rollOverArr=new Array();
function setrollover(OverImgSrc,pageImageName)
{
if (! document.images)return; <--this is the line giving me problems
if (pageImageName == null)
pageImageName = document.images[document.images.length-1].name;
rollOverArr[pageImageName]=new Object;
rollOverArr[pageImageName].overImg = new Image;
rollOverArr[pageImageName].overImg.src=OverImgSrc;
}

function rollover(pageImageName)
{
if (! document.images)return;
if (! rollOverArr[pageImageName])return;
if (! rollOverArr[pageImageName].outImg)
{
rollOverArr[pageImageName].outImg = new Image;
rollOverArr[pageImageName].outImg.src = document.images[pageImageName].src;
}
document.images[pageImageName].src=rollOverArr[pageImageName].overImg.src;
}

function rollout(pageImageName)
{
if (! document.images)return;
if (! rollOverArr[pageImageName])return;
document.images[pageImageName].src=rollOverArr[pageImageName].outImg.src;
}
</SCRIPT>
 

bigdave

Moderator
Staff member
#15
I thik I've emailed Toby directly so often that he's bored and I'm embarrassed. Either that or the repeated references to primates have peed him off. lol.