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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Which method will make the page load faster?

    I have a single webpage that contains information on all 50 U.S. states. There are 50 links at the top to jump down to the state you want, and at the bottom of the information for each state a Back to Top link.

    I'm making the Back to Top link into something more complex, and it will require three or four lines of code.

    So that I don't have to repeat the code 50 times, and create a burden when I need to edit it, I want to place it in a .js file and call it x. Then below the information for each state I'll simply have:

    Code:
    <script language="JavaScript">document.write(x)</script>
    Does calling code from a .js file 50 times slow down the page load? Which method would load faster?

    Or is the difference negligible?

    Thank you,

    Peter

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    It does not matter too much. I would even say that the second method (using document.write()) should be slower.
    Last edited by Kor; 05-12-2011 at 08:35 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    peterinwa (05-12-2011)

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you. I was worried that the document.write might take longer. The question is... is it seconds or something more negligible. I guess I could do a test.

    Peter

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    //
    You could also load the page with a plain html to-top link in each section,
    and add any enhancements onload.

    <div><a href= "#contents"> To Top</a></div>

    // add this to the script in the head that defines x:

    Code:
    window.onload= function(){
    	var A= document.links, who, L= A.length;
    	for(var i= 0; i< L; i++){
    		who= A[i];
    		if(/#contents$/.test(who.href)){
    			//do whatever you want here, eg:
    			who.parentNode.innerHTML=x;
    		}
    	}
    }
    Whatever you do, test it and test it again!
    Last edited by mrhoo; 05-12-2011 at 04:55 PM.

  • Users who have thanked mrhoo for this post:

    peterinwa (05-12-2011)


  •  

    Posting Permissions

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