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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Loading a page on unload?

    Hello,

    Here's what I need to do: when a certain page unloads, I need to load a certain other URL. The page that is to be loaded doesn't have any HTML content, and it would be just as well if the user didn't even know of it being loaded. It's simply a PHP script that updates a record in a database reflecting that the page is being unloaded.

    1. One way I know I could definitely do this is on unload, pop open a popup (or maybe pop-behind) window. Since so many people use popup blockers, though, this isn't ideal.

    2. Another thought I had was that on unload, I could set an IFRAME on the page to point to my script URL. This has worked in superficial testing, but I'm concerned that it may not always, i.e. if the server is taking too long to respond, and the unload takes place before it does.

    Any thoughts?

    Thanks,
    Martin

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,466
    Thanks
    3
    Thanked 495 Times in 482 Posts
    If you are controlling the page to be loaded

    The url could be passed to the php page which is loaded in a hidden iframe

    the php page can have an onload event to load the page it has been passed

  • #3
    New Coder
    Join Date
    Jun 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't have control over the page to be loaded.

    Thanks,
    Martin

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If the page that is to be loaded "doesn't have any HTML content" and you "don't have control over the page"... Why are you wanting to force this upon your unsuspecting visitors?? And why are you trying to do this without "the user didn't even know of it being loaded"...

    What's the purpose of all this... keylogger
    All seems rather malicious...

    .....Willy

  • #5
    New Coder
    Join Date
    Jun 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It is completely beyond me how what I describe could be construed as "malicious."

    This is for a content management system. When the user goes onto the "edit article" page, a "locked" flag is set in the database to indicate that that article is in use, so that another user cannot edit it at the same time. When the user navigates away from the page -- whether he's going to another page within my application or not -- I want to transparently load a PHP page that causes the article to be "unlocked" so that others can work on it.

    Sheesh.

  • #6
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Better to just run the PHP code onunload then.

  • #7
    New Coder
    Join Date
    Jun 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    JavaScript can't run PHP...

  • #8
    Regular Coder
    Join Date
    May 2004
    Location
    Berkeley, California Age: 15
    Posts
    398
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or can it? Javascript can run PHP, in a way, using AJAX. Really, I see this as the only way to do what you're trying to do without requiring a 'transition' page that requires the user to continue on.

    Use the onunload event in javascript to send a request to the server, telling you that the article is 'free'

    AJAX is a technology that allows javascript to load data from the server without reloading the page. Google it, look for it in the wikipedia, and find the webpasties AJAX article. It's brilliant.

    I know a lot about ajax, so if you have any problems post here or contact me
    Jalenack.com .:. YWDA Founder .:. Rounded Corners Maker 1.1! .:. My Blog
    The hardest thing about teaching is not knowing the right answers, but knowing the right questions - Elisabeth Klein
    Pretty buttons does not a great website make.

  • #9
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by babadi
    It is completely beyond me how what I describe could be construed as "malicious."

    This is for a content management system. When the user goes onto the "edit article" page, a "locked" flag is set in the database to indicate that that article is in use, so that another user cannot edit it at the same time. When the user navigates away from the page -- whether he's going to another page within my application or not -- I want to transparently load a PHP page that causes the article to be "unlocked" so that others can work on it.

    Sheesh.
    Sheesh...

    This should be done serverside...
    Javascript is NOT your solution...

    Good Luck;
    .....Willy

  • #10
    Regular Coder
    Join Date
    May 2004
    Location
    Berkeley, California Age: 15
    Posts
    398
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Willy Duitt, how exactly do you propose to do it server-side?

    You would have to rewrite all links to pass through a silent page first. That would destroy the usability of the links. PHP can't tell when a page has stopped being viewed.

    I still see some sort of AJAX implementation as your cleanest option.
    Jalenack.com .:. YWDA Founder .:. Rounded Corners Maker 1.1! .:. My Blog
    The hardest thing about teaching is not knowing the right answers, but knowing the right questions - Elisabeth Klein
    Pretty buttons does not a great website make.

  • #11
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Jalenack
    Willy Duitt, how exactly do you propose to do it server-side?

    You would have to rewrite all links to pass through a silent page first. That would destroy the usability of the links. PHP can't tell when a page has stopped being viewed.

    I still see some sort of AJAX implementation as your cleanest option.

    What the heh do any of the links have to do with anything??
    If anything, change the link to the edit page each time it is visited and an entry is created...
    If you wish to pay me to create such an anti-leaching script... Show me the money!!

    Elsewise, I'm only saying that the solution is certainly not clientside using javascript... Then again, what the heh do I know other than AJAX cleans my toilet real good... If you have a cleaner solution, feel free to freely share it...

    .....Willy

  • #12
    New Coder
    Join Date
    Jun 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Willy Duitt
    What the heh do any of the links have to do with anything??
    If anything, change the link to the edit page each time it is visited and an entry is created...
    If you wish to pay me to create such an anti-leaching script... Show me the money!!

    Elsewise, I'm only saying that the solution is certainly not clientside using javascript... Then again, what the heh do I know other than AJAX cleans my toilet real good... If you have a cleaner solution, feel free to freely share it...

    .....Willy
    I really have no idea what you're talking about. There's no way this could be done not on the client side. The goal is to detect when the client has navigated away from a page. The server doesn't have the foggiest idea when the user navigates away from the page. So I'm clueless as to what you're suggesting here.

    I will investigate the AJAX solution that was suggested. Thank you.

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,960
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Try this:
    Code:
    onunload=function(){
      var php = new Image();
      php.src="unlock.php?id=<? id ?>";
      alert('You chose not to update the article. All entered data was lost.'); //you have to do this to enable the php script to be loaded.
    }
    I don't know PHP so the syntax may be wrong. But you get the idea.

  • #14
    New Coder
    Join Date
    Jun 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    glenngv,

    Very interesting. My only question would be, is there a chance that the unload event could occur before the unlock page loads? For example, what would happen if the server stalls and takes 5 seconds to load the unlock page? Would the unload be delayed until the request goes through or times out, or would it just unload anyway, leaving the article locked?

  • #15
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow untested, but maybe worth a shot ...

    Quote Originally Posted by babadi
    The goal is to detect when the client has navigated away from a page. The server doesn't have the foggiest idea when the user navigates away from the page.
    on every internal click and every form submission set a value to say that the user is still using the app. then onunload, check for the existence of said value, if it doesn't exist then, call your serverside script by opening a new window which itself closes on load. on the otherhand if the boolean does exist, then return false on the unload script. ok!
    *keep it simple (TM)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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