SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Free Geek computerages's Avatar
    Join Date
    Oct 2004
    Location
    /dev/null
    Posts
    1,071
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    js if else statment

    Hello everyone!

    I am prttey new to javascript, and I am having a hard time with javascript's if else statments.... This is what I wana code in javascript

    HTML Code:
    if (main frame's location == 'index.htm') {
    document.write("Index Page");
    }
    Thanks in advance for helping me out!!

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi computerages,

    Here's something for you to experiment with.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <style type='text/css'>
    </style>
    <script type='text/javascript'>
    window.onload = function()
    {
      if (document.getElementById) {
        if (document.location.href.indexOf('index.htm') >= 0) {
          // Example 1
          var ele = document.getElementById('heading');
          ele.innerHTML = 'Index Page';
        }  
      }
    }
    </script>
    </head>
    <body>
    
    <h1 id='heading'>My Site</h1>
    
    <script type='text/javascript'>
    <!-- Example 2 -->
    if (document.location.href.indexOf('index.htm') >= 0) {
      document.write("<h2>Index Page</h2>");
    }  
    else {
      document.write("<h2>My Site</h2>");
    }  
    </script>
    <noscript><h2>My Site</h2></noscript>
    
    </body>
    </html>
    Last edited by MikeFoster; Dec 4, 2004 at 10:53.

  3. #3
    Free Geek computerages's Avatar
    Join Date
    Oct 2004
    Location
    /dev/null
    Posts
    1,071
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeFoster
    Hi computerages,

    Here's something for you to experiment with.
    Thanks for the help, it's greatly appreciated!! Thanks again...

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You just need to figure out:

    main frame's location

    and that depends on what frame the script you posted is in. But, generally you can get the 'main frame' using the 'top' reference:

    top.location.href == "Index Page";

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by MikeFoster
    Hi computerages,

    Here's something for you to experiment with.
    ...
    Wouldn't innerText be better than innerHTML in that instance?

    Or createTextNode() and appendChild()?

  6. #6
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why "better"? I think innerHTML is more widely supported. Besides, it's just something simple to play with.

  7. #7
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry Vinnie, that was my knee-jerk reaction

    computerages, my first post shows two different ways of modifying page content. Vinnie is pointing out that there are two other ways.

    7stud is pointing out that my code has nothing to do with 'frames', which your question does imply the use of.


  8. #8
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's another one
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <style type='text/css'>
    </style>
    <script type='text/javascript'>
    window.onload = function()
    {
      if (document.getElementById && document.createTextNode) {
        if (document.location.href.indexOf('index.htm') >= 0) {
          // Example 3
          var ele = document.getElementById('heading');
          ele = ele.firstChild; // get a ref to the first 'text node' in the h1
          if (ele && ele.nodeType == 3) {
            ele.nodeValue = 'Index Page';
          }
        }  
        else {
          // Example 4
          var ele = document.getElementById('heading');
          var txt = document.createTextNode(' - not the Index Page');
          ele.appendChild(txt);
        }  
      }
    }
    </script>
    </head>
    <body>
    
    <h1 id='heading'>My Site</h1>
    
    </body>
    </html>

  9. #9
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Example 1:
    - Executes after the page has loaded.
    - Modifies h1 content using innerHTML which is not a 'standard' property.

    Example 2:
    - Executes as the page is being loaded (when that script element is loaded).
    - Writes html into the page.

    Example 3:
    - Executes after the page has loaded.
    - Modifies content using only W3C DOM objects and properties.

    Example 4:
    - Executes after the page has loaded.
    - Creates a new 'text node' and appends it to the list of elements contained by the h1.

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again,
    if (main frame's location == 'index.htm')
    Something else to consider when using if statements: if the if-statement looks correct to you, but you know it isn't working correctly, in order to troubleshoot what's wrong, use an alert() on the line just before the if-statement to display the variable you are trying to compare to the string 'index.htm':

    alert(main frame's location)

    Sometimes the variable contains something slightly different than what you expect, so the if-condition fails, when you know it should be true. That's especially the case when comparing page names because a page name might have a full path name preceding the page name--which you might not realize. It happens to me all the time when I'm comparing a variable to a page name: if the script page is not in the same directory as the page name specified in the string, then the path information is incorrect. For instance, 'main page location' might actually be:

    file:///C:/My%20Documents/html/index.htm

    instead of just

    index.htm

    If you use an alert() to display the location, then you can copy down the string that is displayed, and use it on the right side of the '==' comparison to identify that page.

  11. #11
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by MikeFoster
    Why "better"? I think innerHTML is more widely supported. Besides, it's just something simple to play with.
    True, innerHTML gets you IE4 support too I believe. What I meant was that innerText would probably be a better match since you're only modifying text content and not really adding in any HTML

    The DOM methods would be preferable if you're going for purity (and not trying to add to Mozilla's history which using innerHTML does I believe), but they do tend to be slower than innerHTML if you're copying a lot of content back and forth.


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
  •