I have a problem as following...
# I have stored some value in session to reach a Page "A"
# I have other pages "B", "C", "D".
# If I am moving from Page "A" to "B", I should not clear my session value but If I am moving from "A" to "C" or "A" to "D", then I have clear some values in session.
# I have clear it on body unload of "A" on condition. Is there any way to find the URL which I am landing via javascript? Is there any other way or work around to solve this problem.
Note: I have menu in my left frame and content in right frame. All my links to move from "A" to "B", "C" and "D" are in menu.

Recommended Answers

All 5 Replies

The URL of the document that the browser is currently viewing is in the property window.location.href

Actually I wand to find which page I am going load from the current page. Whether It is possible to get that URL from a current page using javascript

In Javascript, you cannot find where the user is going to go next, unless you strictly control the user's movement; something that you can only do within the realms of them navigating using your own controls.

i.e. it's not possible, unless it would suffice to only be notified of such an event if the user goes somewhere using a link directly on your page, in which case, it's easy; just add an onclick handler to all of the links to those other pages. You know where the link is going, because you wrote the link address. If you don't hardcode the link addresses for whatever reason, the link element's href property can be read, and obviously, you could do something more useful than this example with the address: ( i.e. check to see if it is an address for which your session needs to be cleared )

<a href="http://somewhere.com" onclick="alert('Going to: ' + this.href);return true;">Going where?</a>

You cannot, ever, find out if a user is typing in a new URL and is about to hit enter, and browser forwards/backwards actions can't be reliably monitored, nor averted.

At the server, you can find out where the user has been going ( within your site ) by looking at where the user has come from ( REQUEST_URI in the HTTP header ). I don't believe that this is possible in Javascript.

Whether it is possible for me find the same in body unload event. i.e, document.getElementById("xx").href
where tag will look like this
<a href="http://somewhere.com" id="xx">Going where?</a>

When the link has been clicked, put its' id ( or better, the object itself, or its href, or whatever ) in a global variable.

...
<script type="text/javascript">
var last_link = 0;
function unloading()
{
  if( last_link != 0 ){ ... }
}
function set_last_link( obj )
{
  last_link = obj;
}
</script>
</head>
<body onunload="unloading( );">
....
<a onclick="set_last_link( this ); return true;"/>
...

You might be better off using a javascript-controlled cookie all throughout the site; ie. on every page onload event, read the last URL from the cookie, do whatever you'd intend do when the previously loaded page was unloaded, then write the current URL to the cookie. That way, whenever the user moves between pages, you can find out where they came from. That's gotta be more useful, and flexible, and I can't think of any situation where that would be less suitable than trying to find out where the user is going.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.