SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast HitRaj 47's Avatar
    Join Date
    Nov 2005
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question What's wrong with this code? (Loop through array)

    Code:
    var url = window.location.pathname;  
      
    var myPageName = url.substring(url.lastIndexOf('/') + 1);
    
    //pages that shouldn't have the prototype.js script
    var excludedPages = new Array(
    "admin.php",
    "upload.php",
    "catmgr.php",
    "albmgr.php",
    "picmgr.php",
    "searchnew.php",
    "util.php",
    "pluginmgr.php",
    "banning.php",
    );
    
    for ( var i=0, i<excludedPages.length; ++i ){
    	if (myPageName != excludedPages[i]) {
    		document.write('<scr'+'ipt type="text/javascript" src="js/prototype/prototype.js"></scr'+'ipt>');
    	}
    }
    What I am trying to do is not get the prototype.js included on those specific pages, as it messes with the forms. However, for all other pages, the prototype.js is fine to be included.

    I have tried an if statement like this:
    Code:
    if (myPageName != "admin.php") || (myPageName != "upload.php") {
    ...
    }
    But whenever I add a 3rd item, it stops working. I would prefer to have an array as it makes sense and is easy to read, but I can't figure out what's going on.

    What am I missing?
    Beware of Raj - My Portfolio and Blog.

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,329
    Mentioned
    191 Post(s)
    Tagged
    4 Thread(s)
    The trouble with your attempted 'if' statement is "short circuit logic". As soon as a decision can be made (true or false, evaluating from left to right) that answer is returned.

    With your loop BEWARE that excludedPages.length will be the EXACT LENGTH of the array. If you loop through the array beginning with zero (which is necessary to access the very first element) your loop will overrun. It will try to continue until "i" equals one more than the index of the last item.
    The generally accepted syntax is this:
    Code:
    for ( var i=0, i<excludedPages.length -1; ++i ){
    However, if you don't need to iterate the array in sequence, I prefer a shortcut (according to some things I have read this is also slightly faster)
    Code:
    var i = excludedPages.length;
    while (i--) {
    //  .... do stuff
    }
    Because Javascript will evaluate ZERO as a false, this works well.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,786
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by HitRaj 47 View Post

    I have tried an if statement like this:
    Code:
    if (myPageName != "admin.php") || (myPageName != "upload.php") {
    ...
    }
    That code would only ever be true if maPageName were set to both admin.php and upload.php at the same time - which can never happen. To get the resulot you want you need && between the conditions and not ||
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •