Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Importing javascript to multi-htm pages?

    Have now got my basic image tumbnail page created. I have several javascripts that will be used over and over in about 50 tumbnail pages.

    I know there is a way to create a file that is placed on the server that would store these javascripts and then I have each page call or load them, right?

    But I don't know how to create the JS file, or how to import them into a web page. Any help or links to a really good reference would be appricated.

    Also how do I know where is the best place to put my JS code. I have some in the <head> and some in the <body> section but I don't really understand the ryme or reasons for doing so. I have seen some JS code that you can download from other sites that give instruction about putting one part of the code in the <head> section and another part in the <body> section, so I guess it can make a difference.

    Thanks,
    PhotoJoe

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Open notepad and copy your script into it and save it as a .js file

    jsfilename.js

    When you copy your script do not include the opening and closing script tags.

    Then in all the pages that are going to use the script put

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

    in the head section.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Mr J,

    But leads to another question. If I have more than one JS and they don't have any variables with the same name can I combine them into a single JS for the JS file?

    Also is this only for JS that you would normally place in the head section?

    I have a couple of JS that I completly wrote myself and I placed them in the body section. One I call from within the body tag [onload="function()]. Is it better to put this one in the <head> section so I can store in a JS file?

    Thanks again,
    PhotoJoe

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    By all means combine the Javascript into one external file if it is all going to be used on most pages.

    You can make Javascript from both the head and body section of the page external. You just link them into the page at the appropriate point.

    Javascript is best placed in the head of the page except where it needs to go in the body. Whatever is called from the onload can go in the head.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks felgall (Stephen),

    I think I will make a copy of my tumbnail page and start moving the javascripts that I have in the body section to the head section one at a time and check that the page displays the way I want. I might be able to move them all by making the complete script a function and then in the body where I need it write a small bit of javascript that calls the javascript function in the head section.

    Want do you think?

    Thanks again,
    PhotoJoe

    Quote Originally Posted by felgall
    By all means combine the Javascript into one external file if it is all going to be used on most pages.

    You can make Javascript from both the head and body section of the page external. You just link them into the page at the appropriate point.

    Javascript is best placed in the head of the page except where it needs to go in the body. Whatever is called from the onload can go in the head.

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Just remember that if any of your scripts run onload the onload event still has to be included in the opening BODY tag of that page
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #7
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mr J
    Just remember that if any of your scripts run onload the onload event still has to be included in the opening BODY tag of that page
    Thanks Mr J,

    That is what I have done.

    I now have all the heavy javascript coding in two jsfilename.js files. (different names of course) One is a list of current page globol variables define. The other is javascript and functions that use the variables define in the other js file. The javascript and functions js file with be used by all of my tumbnail pages (about 50 of them) I will have a different variable js file for each tumbnail page.

    I'm not sure if it makes a difference but I'm making sure that the variable js file is loaded first (or least it is on the htm page) before the javascript and functions js file is loaded.

    PhotoJoe

  • #8
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    The javascript and functions js file with be used by all of my tumbnail pages (about 50 of them) I will have a different variable js file for each tumbnail page.
    As the different variable js file is going to be unique to each page you could just put them in that page rather than having loads of js files.

    So you would go:

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

    <script type="text/javascript">
    <!--

    thisPagevariable1=whatever1
    thisPagevariable2=whatever3
    thisPagevariable2=whatever3

    //-->
    </script>

    but make sure that the javsacript and functions script tags are put first.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #9
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Mr J,

    Thank you so much for you taking the time to help me.

    Right now I have the following in my <head> section

    Code:
    <!-- ***** Javascript Section Starts Here ***** -->
    	<!-- *** Javascript variables for later use defined here *** -->
    	
    <script type="text/javascript" src="tnVariables.js"></script>
    
    
    	
    	<!-- *** End of Javascript variables definitions         *** -->
    
    
    <script type="text/javascript" src="tumbnail.js"></script>
    
    
    
    <!-- ***** Javascript Section Ends Here    ***** -->
    As you can see my javascript section of the <head> section is quite small. I have also call the js file with the variables first. I thought I should do this so that the global variables use by the function would already be defined. I quess I could try and reverse the two and see if it makes a difference.

    Now for the reason I want to use a js file for the variables. I have in mind trying to figure out how I can have the page that calls the tumbnail page be able to pass the the URL of the variable js file and then have just the one tumbnail page do the job of all 50 tumbnail pages I was going to create. But that is a different problem that I'm going to work on. I will do some research in my reference books and try creating a very simple page to test my thoughts. This forum may be seeing a question or two on this later!!

    PhotoJoe


    Edited note:

    I have tried reversing the call for the js files and it does make a difference. So if a function is going to use variables that are define in another js file, that js file with the variables needs to be call first.

    PhotoJoe
    Last edited by PhotoJoe47; 10-21-2005 at 11:09 PM.

  • #10
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Take a look at this example it may help you out.

    http://www.huntingground.freeserve.c...img_gal4v1.htm

    A single page is used to show numerous galleries.
    When a Showcase link is selected the thumbnails relative to that showcase are dynamically generated
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #11
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mr J
    Take a look at this example it may help you out.

    http://www.huntingground.freeserve.c...img_gal4v1.htm

    A single page is used to show numerous galleries.
    When a Showcase link is selected the thumbnails relative to that showcase are dynamically generated
    Thanks agin "Mr J"

    That javascript is very interesting. It has several new elements of javascript that I need to become more inform about. (I hate when I can't spell the word I really want to use).

    This has gone off subject of this thread so if, or should I say when, I have questions about what is in the example javascript I will start a different thread

    Much thanks "Mr J" and here is a rose for you:

    @=======

    Photo Joe


  •  

    Posting Permissions

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