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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2005
    Location
    TN
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation help with if then.

    hey everyone. i dont even know if what im about to ask is even possible. but i really need some help here.

    im trying to make an alternating link to either close the open window, or to send the browser back one page depending on if anything is in the browser's history or not.

    im sure everyone knows the code for close and back...but here they are anyways just incase:

    <a href="javascript:window.close();">Close Window</a>

    <a href="javascript:history.back();">Back</a>

    of course i am using a small image at the beginning of each string of code but im not sure that makes a difference.

    anyways...what i need is an if then that can read the browser's history...using the history.length() i assume...and determine which code to place on the page based on that information.

    here is what i have tried so far:

    <sript language="Javascript">

    if (history.length() == 0)
    {
    document.write("<a href="javascript:window.close();">Close Window</a>");
    }

    else {
    document.write("<a href="javascript:history.back();">Back</a>");
    }

    </script>

    and...

    if (history.length() < 1)
    {
    document.write("<a href="javascript:window.close();">Close Window</a>");
    }

    else{
    document.write("<a href="javascript:history.back();">Back</a>");
    }

    and...if tried alternating the if and else such as:

    if (histoy.length() > 0)
    {
    document.write("<a href="javascript:history.back();">Back</a>");
    }

    else{
    document.write("<a href-"javascript:window.close();">Close Window</a>");
    }

    if also tried all of those using document.writeln.

    im at a complete stand still.

    the page its going to be used on can be found at the following link:

    http://melted-dreams.net/~spicyfetus/pages/404.htm

    so...any help is greatly appreciate....thanks a ton guys and gals :)

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    try this, put the following code in your head tags
    Code:
    <script  type="text/javascript">
    <!--
    function newhistory(){
    var times=history.length;
    if (times == 0)
    {
    document.getElementById("altlink").innerHTML="<a href='javascript:window.close()'>Close Window</a>";
    }
    
    else {
    document.getElementById("altlink").innerHTML="<a href='javascript:history.back()'>Back</a>";
    }
    }
    //-->
    </script>
    add this onload code to your body tag
    Code:
    onload="newhistory()"
    and where ever you want the link to appear put some kind of element there that has an id of altlink for example if i wanted a link to appear in a table i would do this
    Code:
    <table width="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
    <td>some content here</td>
    <td><span id="altlink"></span></td>
    </tr>
    </table>
    hth

  • #3
    New to the CF scene
    Join Date
    Jan 2005
    Location
    TN
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    awesome...it works perfectly.

    thanks for the help. im really gonna have to analyze your code here to see what you've done. really interesting stuff...it's alot more complex than i had expected it to be.

    could you possibly explain a little of it?

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    sure i'll try to explain it so u can understand it
    here i am just defining the function and declaring a variable, times=history.length
    Code:
    <script  type="text/javascript">
    <!--
    function newhistory(){
    var times=history.length;
    if (times == 0)
    {
    document.getElementById("altlink").innerHTML="<a href='javascript:window.close()'>Close Window</a>";
    }
    
    else {
    document.getElementById("altlink").innerHTML="<a href='javascript:history.back()'>Back</a>";
    }
    }
    //-->
    </script>
    here is the actual if else statement, its basically saying if history.length is equal to 0 then it will write the correct link, and else write the other link the one for history back, into an element that as an id of altlink, innerHTML is more dynamic than document.write but its not DOM compliant but its okay to use for what u are trying to do, u can't use quotes within the innerHTML quotes because that will tell the code that the line is done so u have to use single quotes like i did
    Code:
    if (times == 0)
    {
    document.getElementById("altlink").innerHTML="<a href='javascript:window.close()'>Close Window</a>";
    }
    
    else {
    document.getElementById("altlink").innerHTML="<a href='javascript:history.back()'>Back</a>";
    }
    }
    //-->
    </script>
    then i used the body onload to call the function once as the page loaded so it would write the correct link into the page, there are other ways to do what u want but this was the first way i came up with, u could even get rid of a line by changing this
    Code:
    var times=history.length;
    if (times == 0)
    to just simply this
    Code:
    if (history.length == 0)
    Last edited by _Aerospace_Eng_; 01-20-2005 at 07:16 PM.

  • #5
    New to the CF scene
    Join Date
    Jan 2005
    Location
    TN
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    awesome. i had tried using functions before. but i was still trying to hit around the history.length() for whatever reason...and i had never thought of using on load.

    thanks so much for your help


  •  

    Posting Permissions

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