Results 1 to 6 of 6
  1. #1
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    How do you connect a .js file to the html? (FrontPage 2003)

    Hi,

    I am running a small website and have been adding small javascripts to the webpage. These javascripts animate some text on the site as well as highlight a banner and one or two other little eye-catchers. I am still in a learning curve with html and this website is proving a valuable resource to learn from, (besides the lounge of course! <img src=/S/smile.gif border=0 alt=smile width=15 height=15>). Anyways, I have discovered that one can remove the scripts from the head section and paste it into a .js file and then use a pointer (something like a SPAN tag) to point to the .js file to run the script from this file. This allows one to remove all the code functions from the webpage and clean it up quite a bit. PS: I have seen that one can also have a .css file that runs the style of the script from an external css file too. I really like this approach as it prevents the html of the actual webpage to not get so cluttered with all these functions and styles.

    My question is: I would dearly love to know how to link the .js file to the web page? I got it right by accident with one script while I was experimenting with it (after making a .js file). But with subsiquent tries with other scripts, I have not managed to get this right?? <img src=/S/sad.gif border=0 alt=sad width=15 height=15> Can anyone help me understand the linking dynamics better please...

    I have seen two approaches...
    1. Using script like this in the header...
    <!t>[<link href="JavaFiles/Billboard/Billboard.css" rel="stylesheet">
    <script language="javascript" src="JavaFiles/Billboard/Billboard.js">
    </script>
    ]<!/t>

    and

    2. Using SPAN tags with an id and calling on the function...
    <!t>[ <span id="CharHL1">TEST INFORMATION</span></font>
    <font size="5" color="#FFFFFF" face="Trebuchet MS">
    </font>[/b]</td>
    <SCRIPT language="JavaScript">
    animate('CharHL1', 'yellow'); //this line calls the function in the .js file, called Function animate!
    </SCRIPT>
    ]<!/t>

    TX for any help!
    Regards,
    Rudi

  2. #2
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: How do you connect a .js file to the html? (Fr

    Hi Rudi

    Option 1 is my preferred method as it reduces the clutter and overhead between the head tags and much clearer as you can seperate and have multiple js files

    <link href="JavaFiles/Billboard/Billboard.css" rel="stylesheet">
    <script language="javascript" src="JavaFiles/Billboard/Billboard.js"></script>
    <script language="javascript" src="JavaFiles/Email/Email.js"></script>
    <script language="javascript" src="JavaFiles/Maths/Maths.js"></script>

    My example above shows how I have linked 3 different js files, a habit I picked up from module work in VBA where I group specific types of code together so that I don't have to take a whole bunch of scripts offline or if I change ideas

    Create the code within the head tags, test them and see if they work and then and only then move them to the external js file just be careful though as some ISPs don't support external js files.
    Jerry

  3. #3
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: How do you connect a .js file to the html? (Fr

    Hi Jezza, tx for that warning...I did not know that about cetain ISP's!

    The code you show is a reference to the external file...and is added to inbetween the head tags, but how or what do I put in the body tags to make the animation run at that position in the page?

    TX
    Regards,
    Rudi

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: How do you connect a .js file to the html? (Fr

    Some kind of scripts are meant to work "on the spot." These usually contain code such as <code>document.write("Insert this here")</code>. In other cases, your code contains a function and you can invoke that function elsewhere in your page. It would be easiest to work with an example.

    As an aside, the language attribute is mostly obsolete, unless you need to invoke Microsoft's JScript variant or VBScript. Nowadays, people usually use the type attribute:

    <code><script type="text/javascript" src="myfile.js"></script></code>

  5. #5
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: How do you connect a .js file to the html? (Fr

    TX Jefferson. If I struggle with a particular sample of script I will post back to request help.
    Cheers
    Regards,
    Rudi

  6. #6
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: How do you connect a .js file to the html? (Fr

    Hi Rudi

    Further to what Jefferson has mentioned have a look at this The Type and Language attributes of JavaScript at the bottom of the page are the other chapter headings and is some nice background reading <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    Jerry

Posting Permissions

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