Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I need to show a popup message (couple of div tags) when a user tries to do the following:

  1. Leaves the current SharePoint site and navigates to an external URL.
  2. Closes the browser.

I tried using the JavaScript onbeforeunload event of the window object through which I could popup the message when the user tries to navigate away from the current page.

But my requirement is to only show him the popup when he tries to leave the SharePoint site and not show him the popup when he navigates to any other page in the same site.

Please help!

share|improve this question
    
What if the user has several windows open with your site's pages? You'd throw them your popup in the face every single time or save a cookie just to make sure you don't annoy them too much? –  Alexey Krasheninnikov Jun 25 '10 at 19:43

4 Answers 4

You could accomplish it easily client-side, but that said, it's easy in principle but would be quite client-side intensive if you had a page with a lot of links.

You could do it either with straight JavaScript, or with jQuery. Basically capture the click event of any tag, and inject a function to check the link. Pass it the href value and do some regex to see if the hostname matches the start of the URL. If it does, return true so the click action continues, as the user is just browsing internally. If it does not match, call your alert or confirm dialog telling the user they're leaving.

As for your other scenario, there's no way I know of to detect that the user picked a new bookmark or typed in a new URL; that's all handled within the browser and I don't think there's anything script accessible to detect that.

share|improve this answer

You could capture the OnClick event of all anchor tags and pass the anchor control to the script. Then you need a conditional statement in your script checking the .src of the anchor tag, if its external then show your pop-up, otherwise don't.

Not thought this through entirely, but the concept should work...but I have had a hell of a day today so I probably missed something glaring.

share|improve this answer
    
Yes, like many-many links within the site, especially in the list views, are handled by functions such as GoToLink(elm) and others. Those would conceal the true target of navigation and you would have to somehow intervene before the actual function fires. –  Alexey Krasheninnikov Jun 25 '10 at 19:41
    
Well the issue wont be with the anchor tags but with the user directly typing a new url in the address bar or selecting a bookmarked url. Any suggestions in that scenario? –  Vivek Jun 26 '10 at 4:12

I think there is no systematic way to capture the new URL, simply for privacy reasons.

If the URL change is triggered by an event on the current document (typically click on a link), then you can compare the current and target URLs.

If the user is typing a new URL, then maybe you can safely assume that he/she is moving away from the current flow, and trigger the popup?

share|improve this answer

I don't know if you really need to do this clientside, but you could try to do it serverside. The Session_End() method in the global.asax is the event where you could check such things. This event raises when someone 'leaves' the website.

A little MSDN page about this event: http://msdn.microsoft.com/en-us/library/ee255109(BTS.10).aspx

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.