Forum Moderators: open

Message Too Old, No Replies

Can script be moved to "later in page"

I'd like to move scripts to after content - what are the affects?

         

Shadows Papa

1:02 pm on Nov 12, 2003 (gmt 0)

10+ Year Member



Greetings,
I have some scripts (actually, several) used on some of my pages. Typically, I place them at or near the top of the body in the HTML code.
It works great, I have few issues. However, seems to me I'd be better off maybe having the non-content moved to the end of the files.
For example this script is immediately after the opening body tag, it's used to open a second window to show larger photos of merchandise. They click on the thumbnail and get the full pic:
<script LANGUAGE="JavaScript">
//<--!
function largeview(picture)
{
window.open (picture,"closeview","width=580,height=500,location=0,menubar=0,resizable=1,scrollbars=1,status=1,titlebar=1,toolbar=0,screenX=40,left=40,screenY=20,top=20");
}
//-->
</script>

Here's the href for the above:
<a href="javascript: largeview('view2.asp?name=merchandisepicture')">
<img border="0" src="merchandisepicture-t.jpg"></A>

How will it work if I move the script portion to the end? In a test, it seemed fine in IE, however if the page wasn't done loading, when I clicked on a thumb to show the large picture, the rest of the images for that page simply didn't load! Maybe a fluke with IE on my computer?

I have a similar question for the script code that runs a floating menu on my pages, but I'll post that in a different message.

Thanks

Shadows Papa

dcrombie

3:22 pm on Nov 12, 2003 (gmt 0)



As a rule, you should place any functions in the <HEAD> or at least _above_ the point where they're going to be called from. Otherwise the function can be called (ie. by the user clicking on something) before the function is defined.

The best way to 'delay' execution of a script is to use <BODY ... onLoad="myscript();"> which waits for the page to finish loading and then runs your code.

In this example, you could put the "myscript()" function after your HTML content.

;)

BjarneDM

6:53 pm on Nov 13, 2003 (gmt 0)

10+ Year Member



Declarations of functions really should be in the <head> section. They don't do anything until activated - in your case by clicking on the image.

If the page hasn't loaded fully, then all scripts that haven't been seen by the browser simply won't work.

As dcrombie states, the best policy is to have all scripts in the <head> section. Scripts that ought to be executed after the page has loaded is then called from the <body onload=""> You can call more than one script from there, but I'ld prefer to collect all such function calls in a meta call like this:

function main()
{
myscript1(parameter);
myscript1(parameter1,parameter2);
...
}

<body onload="main();">

On the other hand, I'm using scripts to dynamically generate menus, and these are loaded directly into place at the apropriate places in between the other html code. And on one page I'm enquiring about the number of entries in an array putting the result out on the page, and that piece of code is also placed directly within the other text.

YorkshireSteve

7:05 pm on Nov 13, 2003 (gmt 0)

10+ Year Member



Have you not though of putting all the scripts into their own file?

Having

<script language="javascript" src="scripts/javascript.js"></script>
in my <head> makes things far easier.

If you use the code accross multiple pages, you only need to update the javascript.js file.

Steve.