473,698 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16516
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.bgColo r = 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.bgColo r = 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
9912
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 document: window.location.hash = "#intro"; But in the same browser, this would not work:
5
7528
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 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.
3
3093
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 function) Mozilla/FireFox/Netscape causes the browser to go into an infinite loop, apparently attempting to reload the page. ie: <script type="text/javascript">
3
31935
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 the map, the URL in the address bar of your browser changes to match your current location. How have they done this? I thought calling window.location.href = "etc"; would reload the page, navigating away from the current URL. Yahoo! have...
2
8814
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 for back/forward-buttons. The track I'm working on is "hidden frames" to keep state in window.location.hash. I've seen some solutions along this way that includes polling window.location for a change... which doesn't comply with my otherwise...
4
7899
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
3035
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 window.onload=location.hash('somewhere'); the URL is indeed appended, i.e. www.example.com/index.php#somewhere, but window itself doesn't actually move to the bookmark. However, if I do something like
4
2171
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 can see there. I renewed my site recently using ap div s and dreamweaver in a template and css to get away from my fifteen yr old frontpage one. However it did not work with IE6 as you probably know. So I made a second version for IE6 and tried to...
10
3254
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 where you could improve the application by detecting the browser vendor/version. Some of the posters here disagreed. Since then, I've had to deal with a few of these cases; some of them could be rewritten to use object detection, and some couldn't....
0
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.