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 Coder
    Join Date
    Jan 2003
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question history.go(-1) – what if there ISN’T any –1 to go to?

    Hiya. In need of a little help on a kind of “go back one page OR do something else” function. If that makes any sense...

    At the moment I have:

    {
    alert("Site Closed.\nClick OK to return.");
    history.go(-1);
    }

    The problem is – if the page is the first that the person has visited (they went there directly) then there is no ‘-1’ to send them to. So I’d like something that works along the lines of:

    if no ‘-1’ page exists then parent.close()

    But I’m entirely unsure if and how you can find out that there is no ‘-1’ page and how you’d then trigger the window close event instead.

    Any help would be most appreciated.

    Cheers

    Matthew

    PS: If you could make it thicky-proof that'd be even more helpful

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    (history.length ? history.back : top.close)();

  • #3
    New Coder
    Join Date
    Jan 2003
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you. Fabulous. Does it exactly and perfectly Yay!

    However, I'm now getting a bit carried away by the joy of all this JavaScript stuff...

    Thanks to jkd I now have:

    {
    alert("Site Closed.\nClick OK to return.");
    history.go(-1);
    (history.length ? history.back : top.close)();
    }

    Suppose I wanted to do something other than close window? Maybe flash the background colour of the page to a bright red alert colour... I found this script somewhere; who only knows where now... but say I wanted to use this instead. How would I 'join' this to the 'no history' bit?

    {
    intrvl=0;
    for(nTimes=0;nTimes<3;nTimes++){
    intrvl += 200;
    setTimeout("document.bgColor='#FF0000';",intrvl);
    intrvl += 4000;
    setTimeout("document.bgColor='#FFFFFF';",intrvl);
    }

    This is idle curiosity now. I don't really have any need for it; but since someone probably knows, and it's vageuly interesting (to me) I though I'd ask.

    Cheers again,

    Matthew

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    USA
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    {
        alert("Site Closed.\nClick OK to return.");
        if(history.length){
            history.back();
        } else {
            intrvl=0;
            for(nTimes=0;nTimes<3;nTimes++){
                intrvl += 200;
                setTimeout("document.bgColor='#FF0000';",intrvl);
                intrvl += 4000;
                setTimeout("document.bgColor='#FFFFFF';",intrvl);
            }
        }
    }

  • #5
    New Coder
    Join Date
    Jan 2003
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Yay!

    Hurrah for www.codingforums.com!

    Thanks guys. Most groovy.

    Matthew


  •  

    Posting Permissions

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