Results 1 to 10 of 10

Thread: Loading JS

  1. #1
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default Loading JS

    I currently have something that appends to the head of a file (well js and css).

    Ironically, it works when i upload it to my server:
    http://thebcelements.com/dhtml_test/load_js/index.htm
    However, offline it doesn't work. Meaning the JS doesn't load. I'll post the loading script in here:
    Code:
    $(function(){
    	appendToHead({
    		css: ['css/main.css'],
    		js: ['js/navbar.js','js/timebar.js']
    	})
    })
    
    function appendToHead(a){
    	var css = a.css;
    	var js = a.js;
    	
    	for(var items in css)
    		$('head').append('<link href="'+css[items]+'" rel="stylesheet" />');
    	
    	for(var objects in js)
    		$('head').append('<script src="'+js[objects]+'"></script>');
    }
    Last edited by Deadweight; 08-26-2014 at 03:01 PM.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  2. #2
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    It works offline on my machine. There are 2 identical horizontal menus, though. After I hit HOME, that problem corrects itself.

  3. #3
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    Shouldn't be two menus. I think something was auto included when you saved it. I was stupid because i forgot to http://localhost/websites/Websites/cgs/index.htm
    Lmao. It works also. My bad
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Escape the slash in </script>:

    Code:
    		$('head').append('<script src="'+js[objects]+'"><\/script>');
    If you don't, the script interpreter may conclude that the script is ending there. Once uploaded, the code might be automatically escaped, or encoded in such a way as to not make that conclusion. If it's an external file, it probably doesn't need to be escaped. If it's on the page, it certainly should be.

    BTW, you are iterating over arrays as if they're objects. You can, but it's inefficient. With only one or two members, it doesn't matter so much. It can also lead to assigned prototypical properties being included in the iteration when this might not be intended. Using the numerically based:

    Code:
    for(var i = 0; i < arrayName.length; ++i)
    syntax is generally preferred. Using a numerically based while loop is even more efficient. Either will ignore prototypically assigned properties and only iterate over numerically indexed elements.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    Alright, thanks. I will keep that in mind. For me it was just shorter code and i was wanting to test to see if it it would append the css/js to the head. I switched it an added the slashes.

    Also, for my navbar.js is this the best way to detect the path select file (eg index/about/contact us/etc..):
    Code:
    var pathname = window.location.pathname;//gets path name
    	var finalPath = pathname.split("/")//splits it to an array
    	var firstLink = $('#nav_left>li:eq(0)>a').attr('href');//gets the first a tag in nav bar
    	
    	//checks to make sure the path isnt blank. If blank set it to the first link else get the final 
    	if(finalPath[finalPath.length-1]=='')
    		pathname=firstLink;
    	else
    		pathname=finalPath[finalPath.length-1];
    	//end if
    	
    	$('#nav_right, #nav_left').children('li').addClass('not'); //sets all values in the nav bar to .not
    	$('#nav_main a[href="'+pathname+'"]').parent().addClass('selectedPage').removeClass('not'); //clears not style and sets it to a selected style!
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not really sure I follow. I generally see what it's doing, but without several different example first links and the subsequent changes (if any) that brings to see what the results would be in different situations, I cannot be sure. If it works, as long as that's not a coincidence, I guess it's OK. In general, I would advise against using javascript to set paths. Depending upon the situation, either the absolute, network, or relative path, used in ordinary HTML, should be sufficient.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    Here is an example of what i mean
    http://cgs.thebcelements.com/index.htm
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, now I think I get exactly what you're talking about. Seems fine to me on the use you have put window.location.pathname. The one thing that jumps out at me, and I believe I've noticed this in other code of yours that I've seen but never mentioned it before, is the use of a negative class name (not in this case). Why have a .not class when the resting (not selected) style of the links can just as easily be those with no class or without the selectedPage class. That's how it's generally done. One selector (made up of the elements themselves, like #nav_main li) controls all of the nav links, and the added selector (#nav_main li.selectedPage in this case) adds to that to create the highlight and/or whatever added/changed styles. There's no need for a not selected selector, and therefore it never needs to be assigned or removed. Makes the javascript more efficient.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    The reason why i use .not is for this .not:hover
    If i had #nav_main li:hover it would fail to work. Look at my navbar.js and see how it is made.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I think you could still have #nav_main li:hover and a later #nav_main li.selectedPage:hover that would either negate or use a different hover style (in your case, simply restate the non-hover property/value pairs of the .selectedPage item, but open to change if the user of your code desired). Again, the object is to simplify the javascript and return control of style primarily to the stylesheet.

    Put another way:

    Code:
    #nav_main li {
    	/* basic nav styles here */
    }
    #nav_main li:hover {
    	/* basic nav hover styles here */
    }
    #nav_main li.selectedPage #nav_main li.selectedPage:hover {
    	/* how the selected nav should look regardless of whether it's being hovered or not */
    }
    That said, one of the reasons I never mentioned it before was because it is a finer point.
    Last edited by jscheuer1; 08-28-2014 at 03:18 AM. Reason: detail
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Replies: 3
    Last Post: 02-04-2013, 11:07 PM
  2. loading from db
    By ganu in forum PHP
    Replies: 1
    Last Post: 04-28-2009, 10:18 AM
  3. Resolved Loading JavaScript before page loading?
    By blastbb in forum JavaScript
    Replies: 2
    Last Post: 04-04-2009, 01:31 PM
  4. Replies: 0
    Last Post: 05-23-2008, 02:59 PM
  5. Replies: 0
    Last Post: 05-27-2005, 03:26 PM

Bookmarks

Posting Permissions

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