4

I am attempting to retrieve the hash value for the current page without success. I target a page with a link like this;

http://www.mydomain.com/test.html#hash

My jquery for test.html looks like this;

$(document).ready(function() {
  if (window.location.hash){
      console.log ("FOUND HASH");
  }else{ 
      console.log ("HASH NOT FOUND");
  }
});

No matter what I do, I never get the hash value; it is always empty "". However when I break on the code with Firebug and view the DOM, I can clearly see the hash value is set properly under window.location.hash.

What am I doing wrong?

Thanks in advance for the help.

ANSWER SUMMARY: It turns out cloaking my web address creates a frame wrapper around the entire page and in this case with different ports. The hash was kept by the parent frame but lost to the child and is not accessible by the jquery code. Using the direct non-cloaded address yielded correct behavior.

4
  • what if you take it out of doc ready? what if you just do alert(window.location.hash). I'm sure there must be some syntax error because this should work... what browser are you using? what other context can you give us? Commented May 11, 2013 at 5:39
  • doesn't work inside or outside doc ready. I see this both with FF and chrome consoles. added demo page above. Commented May 11, 2013 at 5:53
  • JFYI, the HTML5 spec has made frame and frameset obsolete elements. w3.org/TR/html5-diff/#obsolete-elements Commented May 11, 2013 at 6:09
  • As mentioned below in the answer comments, the frame was added unbeknowest to me by the web address cloaking mechanism used by my DNS provider. Commented May 12, 2013 at 0:09

3 Answers 3

1

The hash is on your top level frame/window. Your javascript is not in that top level window. So, when you refer to window.location.hash, you're looking at your frame's URL, not the top level window which shows in the browser bar.

And, since your top level window and your internal frame are not the same domain/port, you may not be able to communicate between the two. Make them the same domain and you could get the window.location.hash from the top level window (what shows in the browser URL bar).

If your domains are the same so you don't run into same-origin security restrictions, then you can get the hash from the top level URL with this:

window.top.location.hash

FYI, the top level window has only this in it (that is not where your javascript is):

<HTML><HEAD>
<META NAME="description" CONTENT="robtune.com">
<META NAME="keywords" CONTENT="">
</HEAD>
<FRAMESET border=0 rows="100%,*" frameborder="no" marginleft=0 margintop=0 marginright=0 marginbottom=0>
<frame src="http://www17.robtune.com:8017/test.html" scrolling=auto frameborder="no" border=0 noresize>
<frame topmargin="0" marginwidth=0 scrolling=no marginheight=0 frameborder="no" border=0 noresize>
</FRAMESET>
</HTML>
Sign up to request clarification or add additional context in comments.

3 Comments

crap, where does that come from? I guess I am doing something dumb, but I don't exactly understand what. back to school I go...
AHA! I think this is a byproduct of using "webforward" and/or "cloaking" in Zonedit's DNS settings. All requests for *.mydomain.com redirects to www17.mydomain.com:myport which points to my webserver.
@Rob - yep, that sounds like the right analysis. You probably need to set up a CNAME to your web server's IP address so the domain goes directly to your web server instead of using the cloaking.
1

You can try this:

window.parent.location.hash

1 Comment

This does not work, likely because the parent frame has a different port as mentioned above.
0

are you using the IFRAME in the page and trying to get the hash ??

and please give us the demo URL so that it will help you get the answer quickly. Updated :

You can use :

window.parent.location.hash to get the hash value.

5 Comments

Ohh man , you are using FRAMESET and the FRAMES , that is the reason why you are not receiving the HASH. You can try window.parent.location.hash
ack...apparently I am, but I don't understand why.
@Rob : each frame will have it own document and it will run by its own. for example , you click any link on the frame it will redirect to other page without doing anything to the parent body. it will behave as it is running in a separate window. and the URL provided for this window does not have the HASH value "yourdomain.com/test.html" which doesnt have the hash value .. so you were getting HASH NOT FOUND ..
but when you use the firebug , it was showing you the HASH VALUE for the PARENT WINDOW not the FRAME .. but when you use the window.parent.location.hash the frame will understand that you will need the hash value for the parent URL and it will give you the defined hash value.
Thanks for the explanation, but the part I did not understand was how I was using frames when it was certainly not in my HTML. I have since figured out that the frame was being added as the mechanism of cloaking my web address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.