从 jQuery .load() 检索 URL
我想在以下设置中检索 page2.html 的 URL(完整路径):
page1.html
----------
<script type="text/javascript" src="js/jquery_1.6/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#content").load("page2.html");
}
</script>
<div id='content'>
<!-- this is where page2.html will be placed -->
</div>
----------
page2.html
----------
<script>alert(location.pathname)</script> //this should return the full path of page2.html, but now it returns page1.html
This is a line of content that will be loaded in page1.html
----------
我的问题是,每当我使用像 document.URL、window.location 或 location.pathname 这样的 url 检索器时,它都会返回 page1.html 的完整路径名。这可能是因为 page2.html 是在 page1.html 中加载的,即使我确实在 page2.html 中请求了 URL。
任何人都有解决方案,或者在使用 .load() 功能时这是不可避免的?
I would like to retrieve the URL (full path) of page2.html in the following setting:
page1.html
----------
<script type="text/javascript" src="js/jquery_1.6/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#content").load("page2.html");
}
</script>
<div id='content'>
<!-- this is where page2.html will be placed -->
</div>
----------
page2.html
----------
<script>alert(location.pathname)</script> //this should return the full path of page2.html, but now it returns page1.html
This is a line of content that will be loaded in page1.html
----------
My problem is that whenever I use url retrievers like document.URL, window.location or location.pathname it returns the full pathname of page1.html. This is probably because page2.html is loaded within page1.html, even though I do request the URL in page2.html.
Anyone has a solution for this or is this inevitable while using the .load() functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
既然您想在页面加载后执行一些 JavaScript 来检测路径,为什么不直接使用回调来显示已有路径的值:
Since you want to execute some JavaScript to detect the path after the page is loaded, why not just use a callback to display the value of the path you already have:
您可能需要在隐藏的 div 或 page2.html 上填充 URL(可能通过某种服务器端技术),或者您需要自己构建 page2.html 的完整路径(假设您已经知道 page1 的路径)。 html 和 page2.html 是相对的。
You would either need to populate the URL in a hidden div or something on page2.html, probably via some server side technology, or you will need to construct the full path for page2.html yourself, given you already know the path to page1.html and page2.html is relative to it.
javascript 在加载发生后执行,因此警告 page1 是正确的行为。我能看到的唯一方法是将位置手动存储在第 2 页的 javascript
var
中。The javascript is executed after the load has occured therefore alerting page1 is correct behaviour. The only way I can see around that is storing the location manually in a javascript
var
on page 2.您的
不会给您
page2.html
因为您只是加载 page2.html 的内容而不是页。我的意思是,如果您在 iframe 中加载 pag2.html,那么您将得到page2.html
。现在内容位于 page1.html 上,因此它会提醒您 page1.htmlyour
<script>alert(location.pathname)</script>
will not give youpage2.html
because you are just loading the contents of page2.html not the page. I mean if you load pag2.html in a iframe then you will getpage2.html
. Right now the contents is on page1.html so it will alert you page1.html我认为你不能借助 Javascript 来做到这一点。 使用load,您不是渲染页面,而是获取内容。
使用
.load
实际上是将数据从URL获取到元素。它使用 AJAX 调用来获取数据并将其转储到“#content”元素上,然后执行您的代码。此时,URL 读取为 page1.html,而不是 page2.html。
I think you cannot do this with the help of Javascript. Using load, you are not rendering the page but fetching the content.
The use of
.load
is actually getting the data from the URL to the element. It uses AJAX call to fetch the data and dump it on the "#content" element and then your code is executed.At this point of time, the URL reads page1.html and not page2.html.
发生这种情况是因为动态加载的数据被插入到当前页面中。如果您确实需要每次都有正确的 URL,请使用 iframe:
但是,如果使用此方法,iframe 中的数据将只有 300 像素高,如果内容较长,则会显示滚动条。
要访问 iframe 的内容,请使用
$("#page2_frame")[0].contentWindow
,它将返回 iframe 的窗口对象。广告@m
This happens because the data loaded dynamically is inserted into the current page. If you really need to have the correct URL every time, use an iframe:
However, if you use this method, the data in the iframe will only be 300 pixels high and a scroll bar will be shown if the content is longer.
To access the content of the iframe, use
$("#page2_frame")[0].contentWindow
, which will return the window object for the iframe.Ad@m
您想要获得的功能可以使用
iframe
而不是div
来实现。The functionality you want to get can be achieved using
iframe
instead of adiv
.