Page redirection based on an event

Discussion in 'Javascript' started by justaguy, Jul 30, 2011.

  1. justaguy

    justaguy Guest


    I wonder if there's a way to cause a page redirection based on an
    event of the current page.

    What I'd like to happen is:
    If the user does not click on the [Download] button I'll redirect him/
    her to a tips page, namely,
    the best-note-taking-tips-from-10-colleges.html file
    in the review directory

    My attempt is:
    include a hidden field named 'downloadChk' in the form of 'f'
    with default value of 0
    if the download button is clicked, change that field's value to 1
    (business role: in that case no need for redirection),
    then with the event of onbeforeunload at BODY element to determine the
    page redirection or not...

    Not working

    Here's the page,

    What am I doing wrong? How to fix it?

    Many thanks.
    justaguy, Jul 30, 2011
    1. Advertisements

  2. justaguy

    Luuk Guest

    my javascript skills are limited.... ;)

    but i have two suggestions:
    <body onbeforeunload="if (document.f.downloadChk.value=='0'){gotoTips();}">

    should be changed to:
    <body onbeforeunload="if (document.f.downloadChk.value=='0'){gotoTips();
    return false}">

    otherwhise the docuument is unloaded.....

    function gotoTips()

    should be changed to:
    function gotoTips()

    Because ther's no such thing as document.href.location.
    Luuk, Jul 30, 2011
    1. Advertisements

  3. Certainly, with the usual caveats
    So you want something to happen in the _absence_ of an event. That's
    something completely different.

    You could set a timer and trigger some action if the user does not click
    on the button within some period of time. Usual and unusual caveats
    apply: WCAG 2.0 has a lot (mostly sensible things) to say about timed
    events and their accessibility problems.

    Or you could trigger an action if the user performs some action without
    having clicked on the button before that. This is almost trivial: use a
    logical variable and set it when the button is clicked on, then test
    that variable in the other event handler.
    I don't see why you would need a hidden field, or a form at all.
    Sounds like my second scenario.
    You have:

    function gotoTips()

    It should have document.location and an absolute URL like

    But I'm not sure whether this is the way onbeforeunload is supposed to
    be used. At least it does not work that way on Firefox. Things change if
    the event handler returns a value.

    Even then, it's problematic. Trying to force people into going a
    specific page when they are about to leave your page in any way won't be
    a success, and it has technical problems as well.
    Jukka K. Korpela, Jul 30, 2011
  4. justaguy

    justaguy Guest

    Thank you both. It works with IE9 but not Firefox 3.6.18 nor Chrome
    IE9 setting for browsing history is "Every Time Loading the Page" (no
    Firefox (unclear, however, the History option of changing the default
    (Remember History) to "Never remember history" does not help.
    Chrome does not even have such a setting...
    justaguy, Aug 5, 2011
  5. justaguy

    dhtml Guest

    document.location is proprietary.

    It isn't.
    Yep. It's a bad idea. Give the user a link instead; he'll click it if
    he wants to.
    dhtml, Aug 7, 2011
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.