473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

detection change of location.hash

Hi,
A page I have shows a different background colour depending on the
hash portion of the url as it is first loaded. For example a link to
mysite/mypage#0000FF would result in a page with a blue background. But
another link, this one to mysite/mypage#FF0000, would not give me a red
background if directed to the window where mypage#0000FF was loaded
just one moment ago. This is normally to be expected, because the browser
thinks same page, no load event, basta.
If I use the search portion, for obvious reasons, that is treated a new page
load, even when it is from the cache, but I need the hash here. So how do I
detect in mypage the moment when the hash string is changed by a user click
event on another page in another window, perhaps even from another domain?

Hope this is clear, thanks for any ideas,
Thomas



Aug 18 '05 #1
7 16491
Lee
User said:

Hi,
A page I have shows a different background colour depending on the
hash portion of the url as it is first loaded. For example a link to
mysite/mypage#0000FF would result in a page with a blue background. But
another link, this one to mysite/mypage#FF0000, would not give me a red
background if directed to the window where mypage#0000FF was loaded
just one moment ago. This is normally to be expected, because the browser
thinks same page, no load event, basta.
If I use the search portion, for obvious reasons, that is treated a new page
load, even when it is from the cache, but I need the hash here. So how do I
detect in mypage the moment when the hash string is changed by a user click
event on another page in another window, perhaps even from another domain?

Hope this is clear, thanks for any ideas,


So, if I understand correctly, you'd like a small pet that sits in
a cage and sings, but it has to be a horse. Is that about right?

Aug 18 '05 #2
"Lee" wrote
User said:
A page I have shows a different background colour depending on the
hash portion of the url as it is first loaded. For example a link to
mysite/mypage#0000FF would result in a page with a blue background. But
another link, this one to mysite/mypage#FF0000, would not give me a red
background if directed to the window where mypage#0000FF was loaded
just one moment ago. This is normally to be expected, because the browser
thinks same page, no load event, basta.
If I use the search portion, for obvious reasons, that is treated a new
page load, even when it is from the cache, but I need the hash here. So
how do I detect in mypage the moment when the hash string is changed by
a user click event on another page in another window, perhaps even from
another domain?
Hope this is clear, thanks for any ideas,


So, if I understand correctly, you'd like a small pet that sits in
a cage and sings, but it has to be a horse. Is that about right?


Close, but not quite. Rather a little mouse like Jerry that runs around the
room endlessly checking the plinth for any holes where it can hide for Tom's
teeth that may suddenly out of the naked blue come hunting him. But I don't
want to use a setTimeout or interval to continuously monitor the addressbar
when not absolutely necessary.
Take for example the case of a person reading this post in whatever program
he uses to read newsgroups, and clicking this link to
http://www.jibbering.com/faq/#FAQ4_40. Once there, he can click on to
#FAQ4_41 or other in-page targets, moves that affect the window history but
not any clear Javascript event. You can set up event listeners around every
link on the page to catch the event of such in-page moves, but now suppose
our user blurs the browser window, returns to this posting and clicks on
this link http://www.jibbering.com/faq/#FAQ4_41 instead. The browser pops
back up, he ends up at the same spot, but the event has not been captured
because it occured in his newsreader client where he follows the newsgroup!
And I don't see how Javascript can ever know about it other than by
constantly checking the location.hash property, wasting enormous amounts of
CPU cycles.

I find that very strange. There must be a way. I don't see how security
would be compromized by this piece of knowledge, it is a bit of information
about the page as obvious and as basic as the window size. There is a
onresize event, why not a onhashchange event?

Thanks again,
Thom


Aug 18 '05 #3
Hi User,

User wrote:
[snip]
You can set up event listeners around every
link on the page to catch the event of such in-page moves, but now suppose
our user blurs the browser window, returns to this posting and clicks on
this link http://www.jibbering.com/faq/#FAQ4_41 instead. The browser pops
back up, he ends up at the same spot, but the event has not been captured
because it occured in his newsreader client where he follows the newsgroup! [snip]
Thanks again,
Thom


One solution that I'm thinking of off the top of my head is what you
already described.

1. Set up event listeners in the page for which the background color
will change. (No need to set up listeners on other pages, because once
they open up this page, the window.location.hash will kick in)
2. Try something like below for the blurring and focusing of the
window.

<script type = "text/javascript">
function bgChange()
{
document.bgColor = window.location.hash;
}

window.onload = bgChange;
window.onfocus = bgChange;
</script>

Hope this helps. :)

Aug 18 '05 #4
User wrote:
Hi,
A page I have shows a different background colour depending on the
hash portion of the url as it is first loaded. For example a link to
mysite/mypage#0000FF would result in a page with a blue background. But
another link, this one to mysite/mypage#FF0000, would not give me a red
background if directed to the window where mypage#0000FF was loaded
just one moment ago. This is normally to be expected, because the browser
thinks same page, no load event, basta.
If I use the search portion, for obvious reasons, that is treated a new page
load, even when it is from the cache, but I need the hash here. So how do I
detect in mypage the moment when the hash string is changed by a user click
event on another page in another window, perhaps even from another domain?

The only way I've found is to use setInterval with a function that
checks it current status. I'd love to find a way to do this in a more
event driven way though, so if you find something please share :-)

http://www.unfocus.com/Projects/HistoryKeeper/

BTW, just to point out a browser bug that you might run into - if the
user manually updates the hash in IE or Mozilla, scripts will no longer
be able to update that portion of the url.

Kevin N.
Aug 18 '05 #5
User wrote:
Take for example the case of a person reading this post in whatever program
he uses to read newsgroups, and clicking this link to
http://www.jibbering.com/faq/#FAQ4_40. Once there, he can click on to
#FAQ4_41 or other in-page targets, moves that affect the window history but
not any clear Javascript event. You can set up event listeners around every
link on the page to catch the event of such in-page moves, but now suppose
our user blurs the browser window, returns to this posting and clicks on
this link http://www.jibbering.com/faq/#FAQ4_41 instead. The browser pops
back up, he ends up at the same spot, but the event has not been captured
because it occured in his newsreader client where he follows the newsgroup!
And I don't see how Javascript can ever know about it other than by
constantly checking the location.hash property, wasting enormous amounts of
CPU cycles.


I've tried to use location.watch('hash',...) on Mozilla (and Netscape
4.x, which if I remember correctly actually works), but it doesn't
trigger when the hash changes, until it is accessed - probably because
location.hash is a property with a getter and setter in Mozilla - which
I even tried to redefine. (Is it possible to monitor whether a specific
method is called? Probably not I'd bet.)

I've never found a way to get it working in any other browsers except to
constantly monitor it (which doesn't even always work - like in Safari).

Sorry for double posting...

Kevin N.
Aug 18 '05 #6
web.dev wrote:
One solution that I'm thinking of off the top of my head is what you
already described.

1. Set up event listeners in the page for which the background color
will change. (No need to set up listeners on other pages, because once
they open up this page, the window.location.hash will kick in)
2. Try something like below for the blurring and focusing of the
window.

<script type = "text/javascript">
function bgChange()
{
document.bgColor = window.location.hash;
}

window.onload = bgChange;
window.onfocus = bgChange;
</script>

Hope this helps. :)


This is an interesting idea - are there events fired when the user
presses the forward and back buttons?

Kevin N.
Aug 18 '05 #7
User wrote:
Hi,
A page I have shows a different background colour depending on the
hash portion of the url as it is first loaded. For example a link to
mysite/mypage#0000FF would result in a page with a blue background. But
another link, this one to mysite/mypage#FF0000, would not give me a red
background if directed to the window where mypage#0000FF was loaded
just one moment ago. This is normally to be expected, because the browser
thinks same page, no load event, basta.
If I use the search portion, for obvious reasons, that is treated a new page
load, even when it is from the cache, but I need the hash here. So how do I
detect in mypage the moment when the hash string is changed by a user click
event on another page in another window, perhaps even from another domain?

Hope this is clear, thanks for any ideas,
Thomas


In css there is a :target pseudo element that can be used to cause the
style of the anchor that is targeted by the hash to change when that
hash is active.

So I guess the question is, is there a way to detect when the target
changes?

Or is it possible to detect when the style of the anchor element(s)
changes (or when the applied pseudo element changes)?

This would only work in Mozilla and other browsers that support the
:target pseudo element, but it's a start.

Kevin N.

http://www.unfocus.com/Projects/IE7/...t.html#oranges

Note: IE7 seems to just trap mouseup events on everything, so this would
not work for your (or my) purposes (pressing the back button does not
update the target in IE with Dean's IE7).
Aug 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Yimin Rong | last post by:
Does anyone know if there are any browsers where you must specify "#" as a prefix when setting the hash for the location? For example, the following would move to the intro section of the...
5
by: spam_me_ not | last post by:
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...
3
by: Stewart | last post by:
Dear javascripters, Through a frustrating afternoon of debugging I appear to have discovered something: Setting location.hash to an empty string in the global namespace (not inside a...
3
by: Paul Neave | last post by:
Yahoo! has launched a beta of it's new mapping application: http://maps.yahoo.com/beta/ It's based in Flash, but it uses JavaScript. I'm curious about one feature, though - when you pan about...
2
by: Dennis Ålund | last post by:
Is it possible to notice a change of window.location.hash without polling? I'm working on a Ajax-platform (yes, inventing the wheel again) and have finished almost everything except the support...
4
by: akdb8r | last post by:
Is there a way for JavaScript to detect when the user changes the URL hash (location.hash)? Thanks in advance! :)
2
by: danep | last post by:
Basically, my problem is exactly as described in the subject. The problem is somewhat intermittent and unpredictable, but the majority of the time if I just have a statement such as ...
4
by: sjpolak | last post by:
sorry, I put the original text instead of my changed one in the previous mail sorry hello, I am new to this forum and a laymen. I have awebsite www.earlyflute.com. I make baroque flutes as you...
10
by: Conrad Lender | last post by:
In a recent thread in this group, I said that in some cases object detection and feature tests weren't sufficient in the development of cross-browser applications, and that there were situations...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.