jquery不返回网址哈希 [英] jquery not returning url hash

查看:108
本文介绍了jquery不返回网址哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为当前页面检索哈希值而没有成功。我使用这样的链接定位页面;

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

我的jquery for test.html是这样的;

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");
  }
});

无论我做什么,我都不会得到散列值;它总是空的。但是,当我用Firebug分解代码并查看DOM时,我可以清楚地看到散列值在 window.location.hash 中正确设置。

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.

我做错了什么?

感谢您的帮助。

回答摘要:
事实证明,我的网址掩盖了在整个页面周围创建一个帧封装器,在这种情况下,使用不同的端口。散列由父框架保留,但遗失给孩子,并且无法通过jQuery代码访问。使用直接非加载地址产生正确的行为。

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.

推荐答案

哈希位于顶级框架/窗口上。您的JavaScript不在该顶级窗口中。因此,当您引用 window.location.hash 时,您正在查看您框架的网址,而不是浏览器栏中显示的顶级窗口。

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.

而且,由于您的顶级窗口和您的内部框架不是相同的域/端口,您可能无法在两者之间进行通信。使它们成为同一个域,并且您可以从顶级窗口获得window.location.hash(显示在浏览器URL栏中)。

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

仅供参考,顶级窗口中只有它(这不是javascript的地方):

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>

这篇关于jquery不返回网址哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆