Current location: Hot Scripts Forums » General Web Coding » JavaScript » Where am I navigating to at the onUnload event?


Where am I navigating to at the onUnload event?

Reply
  #1 (permalink)  
Old 05-21-06, 04:33 PM
oliveirard oliveirard is offline
Newbie Coder
 
Join Date: May 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Where am I navigating to at the onUnload event?

Can I know at the onUnload event where am I navigating to? The window.location.href points still to the page being displayed and not to the next one.

Thanks!
Reply With Quote
  #2 (permalink)  
Old 05-21-06, 04:36 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
I'm not sure what you mean.
window.location.href will always point to the current page being viewed in that window.
If you set window.location or window.location.href to something else, the page in that window will reload (after firing the onunload event) and the window.location.href will be set to the new page.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #3 (permalink)  
Old 05-22-06, 06:56 AM
oliveirard oliveirard is offline
Newbie Coder
 
Join Date: May 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Web page filter

Hi TwoD! Thanks for your reply! Maybe I didn't sound clear on my post. The thing is I want to know if there is any function that knows, at the onUnload event, where the user is navigating to. But as you said, I won't get nothing looking after window.location, right? So I will state here exactly what I want and hope you pros could help me on this.

I want a client-side solution for a web page filter. It would act like a java filter, but not on the server-side. I need this filter so a user can call any web page through it and the filter would make adjustments on the final page presentation. On this initial state, I was using frames and the onLoad event for the main frame (presenting the web page to be filtered). Every time the event is triggered, it's because a new web page is presented on the main frame so I call this filter function to make the adjustments. The main problem is that, first, the page is loaded and only then I can make the adjustments. This is not good considering user interaction issues, so I was trying to use AJAX. I wondered to use the onUnload event to know where the user is navigating to and call it through AJAX, using XMLHttpRequest to get the page, format it, and only then presenting it as the final state of art. Anyway, I was told that "probably" there isn't a way to know, at the onUnload event, where the user is navigating to.

Do you guys have a better idea to solve this? Any suggestion is valuable. Thanks for your attention!
Reply With Quote
  #4 (permalink)  
Old 05-22-06, 07:12 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
onUnload is triggered before the page switch happens. If you have a script running, the switch won't happen until that script is done executing.
A timeout onUnload won't help either since the garbage collection would clear that from memory when switching to a new page.
I'd suggest you call the page using xmlHttpRequest and on the onLoad even for the xmlHttpObject, you parse the code before writing it to the main frame's body.

Note that JavaScript has no ability to change things on documents form outside your domain, due to security issues, when running in a browser. If you want to process external pages, it must happen on the server to be secure.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #5 (permalink)  
Old 05-22-06, 08:46 AM
oliveirard oliveirard is offline
Newbie Coder
 
Join Date: May 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Ok TwoD! Thanks again for your answer!

Did you see my problem now? Your suggestion (to use XMLHttpObject and parse the code before writing to the frame) is exactly what I was considering to do, but my problem is to know what page will be called. The user may click an anchor or write an url at the address bar and I need to know what is this next url. I didn't design the current page being displayed by the filter, so it's complicated to parse all the code and re-address the href's to a certain javascript function that could give me the next url at the unload event because some pages call other functions to decide whether to go. See?

I would really appreciate if you or anyone here could suggest me anything to solve my problem with this client side filter.

Thanks and best wishes!
Reply With Quote
  #6 (permalink)  
Old 05-22-06, 09:58 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
EDIT: Nvm, I didn't read your last post. This won't do what you wanted.

You could try this.

Code:
<script type="text/javascript">

window.onload = function()
{
	var a = document.getElementsByTagName('a');
	var host = this.location.hostname.replace(/www./, '');
	
	for (i = 0; i < a.length; i++)
	{
		if (a[i].href.indexOf(host) == -1)
		{
			a[i].href = 'external.php?url='+ a[i].href;
		}
	}
}

</script>
This will redirect all links on your page that go to external sites to a PHP page that will log the url in an txt file and redirect to the site the visitor actually wanted to see.

external.php
PHP Code:

<?php


if (isset($_GET['url']))
{
    
$fp fopen('urls.txt''a+');
    
fwrite($fp$_GET['url'] ."\n");
    
fclose($fp);
    
    
header("Location: "$_GET['url']);
}

?>
I think this is what you wanted more or less?

Last edited by nico_swd; 05-22-06 at 10:27 AM.
Reply With Quote
  #7 (permalink)  
Old 05-22-06, 12:14 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
If the user types something in the addressbar, no script can detect that. But if a link is followed, you can intercept the redirect and do it your way by parsing all the links onload, like in the code posted. A script has on way of knowing where the browser will be sent to, so a site can't block you from going to some other site.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Place Your Event Script craftyweasel Script Requests 0 04-05-06 12:25 AM
Event date database with reminder and... meslemicha Script Requests 0 03-25-05 07:10 PM
Body onload event with script tag Ruby HTML/XHTML/XML 1 03-07-05 11:25 AM
HELP with printing event month in loop snappy ASP 1 03-01-05 09:54 PM
image's onload event behaves strangely davidklonski JavaScript 0 07-07-04 11:20 AM


All times are GMT -5. The time now is 11:10 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.