Compaibility issue: window.location mismatch with browser locationbar

G

Guest

I already understand that one cannot disable a browser's
forward and back functions. This is a situation where I
have code working in Mozilla V1.6 and would like something
similar for Opera and IE.

I link within a page and display individual divisions of
that page, manipulating their visibility and display styles
with an onClick function. As long as I explicitly click a
link to progress, it works with browsers I've tried.

If I click the browser's back button I see the content of
the browser's location bar change as I expect. But I
need to set the visibility and display styles for the
corresponding division. In Netscape/Mozilla I have a little
function enabled by setInterval(). It looks to see if
window.location.hash matches the division which is visible
and modifies styles accordingly.

Under IE window.location.hash seems to be "stuck" at the
most "forward" link. Under Opera I can get only the URL
without the hash; it claims that the hash property is
undefined or empty. I've tried document.location and
document.URL with the same results.

Since there's no page load or unload involved, there's no
event generated; I could kludge some history of my own to
traverse.

Am I looking in the right place to get the hash info? Is
there some direct access to read the location bar?

Is there another approach to the problem, other than breaking
up the page so that I force a load or unload?

Thanks for any help.
 
R

Randy Webb

spam_me_ not said:
I already understand that one cannot disable a browser's
forward and back functions. This is a situation where I
have code working in Mozilla V1.6 and would like something
similar for Opera and IE.

I link within a page and display individual divisions of
that page, manipulating their visibility and display styles
with an onClick function. As long as I explicitly click a
link to progress, it works with browsers I've tried.

If I click the browser's back button I see the content of
the browser's location bar change as I expect. But I
need to set the visibility and display styles for the
corresponding division. In Netscape/Mozilla I have a little
function enabled by setInterval(). It looks to see if
window.location.hash matches the division which is visible
and modifies styles accordingly.

Under IE window.location.hash seems to be "stuck" at the
most "forward" link. Under Opera I can get only the URL
without the hash; it claims that the hash property is
undefined or empty. I've tried document.location and
document.URL with the same results.

Read the document.location.href and then parse it yourself. You should
know the current URL, simply remove that from the string and then read
the rest.

document.location.search might be an alternative, it would require you
use the ? instead of the #
 
G

Guest

...
Read the document.location.href and then parse it yourself. You
should > know the current URL, simply remove that from the string and
then read > the rest.
document.location.search might be an alternative, it would require
you > use the ? instead of the #

Thanks for the thoughts. But document.location.href shows the
same situation as document.location.hash and parsing it myself
would make no difference. (In fact, for debugging I displayed
..href so I could be sure of what was going on.)

I tried passing the information in document.location.search.
That causes other behaviors in IE and Opera but I still get
the same basic problems I had before.

I decided on a different approach. If the navigation history
within the page is actually useless since the navigation bar
is always available, then window.location.replace() will
avoid a history. The back button goes to my page's referrer,
and forward would go to some other page. That also allows me
to get rid of the setInterval() and the routine it invokes.

Again, it works in Mozilla/Netscape, but not in IE and Opera.
IE *keeps* the history, and the browser's back function leaves
the wrong section visible. Opera does the right thing with
the history but insists on forcing visibility and display
for the division I'm leaving. I have an alert() immediately
after the assignment into window.location so I can see the
*correct* division displayed while the alert() is active. I
then see it revert to the division I'm leaving once I dismiss
the alert().

Here's a code snippet that shows the basic setup:

var idList = new Array("sched", "lead", "cont");

function swapVisibility(theId) {
document.location.replace("veeblefetzer.html#" + theId);
var hideIt;
for (var i=0; i<idList.length; i++) {
hideIt = document.getElementById(idList);
hideIt.style.visibility = "hidden";
hideIt.style.display = "none";
}
var becomeVisible = document.getElementById(theId);
becomeVisible.style.visibility = "visible";
becomeVisible.style.display = "block";
alert(document.location.href+"\n"+theId);
return true;
} // end function swapVisibility

<a onclick="swapVisibility('sched');" href="#sched">...</a>
<a onclick="swapVisibility('lead');" href="#lead">...</a>
<a onclick="swapVisibility('cont');" href="#cont">...</a>

<div id="sched">
...
</div>
<div id="lead">
...
</div>
<div id="cont">
...
</div>

etc.

Am I using document.location.replace() correctly?
 
R

Richie

I think I can describe the solution to the problem as
breaking out of a Netscape V4 mindset. Instead of
changing visibility as part of an anchor tag, I use
the onClick within a paragraph tag. That immediately
gets rid of the navigation history in all browsers.
document.location.replace() is gone.

There's an option to view all sections at once. If
that's enabled, I shove the target location (hash)
into document.location. That gets back into the
navigation history again, but that's acceptable in
what is now a longish page.

The browser incompatibilities, well, chalk 'em up to
something else to remember for future work. This
approach is much cleaner, not just a "workaround."
 
G

Grant Wagner

Richie said:
I think I can describe the solution to the problem as
breaking out of a Netscape V4 mindset. Instead of
changing visibility as part of an anchor tag, I use
the onClick within a paragraph tag. That immediately
gets rid of the navigation history in all browsers.
document.location.replace() is gone.

location is a property of the default window object, so it's
window.location.replace(). document.location is deprecated, has
been for a long time.

<url:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/document.html
/>
"Do not use location as a property of the document object; use
the document.URL property instead. The document.location
property, which is a synonym for document.URL, is deprecated."
 
R

Richie

Grant said:
Richie wrote:




location is a property of the default window object, so it's
window.location.replace(). document.location is deprecated, has
been for a long time.

<url:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/document.html
/>
"Do not use location as a property of the document object; use
the document.URL property instead. The document.location
property, which is a synonym for document.URL, is deprecated."

I tried all three variations along the way. Apologies for
using the deprecated property in my description.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top