如何在调用window.location.href后执行脚本?
我有一个脚本可以将用户重定向到另一个页面。我想在新页面完全加载后将一些内容加载到新页面上的div中。我怎样才能做到这一点。以下不起作用。
I have a script that redirects the user to another page. I want to load some content into a div on the new page after the new page has fully loaded. How can I do this. The following doesn't work.
function goToPage() {
window.location.href = 'http://www.mypage.com/info;
$('.my_class').load('my/url/path/with/content/to/load');
}
新加载的页面( http://www.mypage.com/info )包含以下div:
The newly loaded page (http://www.mypage.com/info) contains the following div:
<div class="my_class"></div>
我做错了什么?
重定向到新页面,但是向URL附加哈希信号。
Redirect to the new page, but append a hash signal to the URL.
function goToPage() {
window.location.href = 'http://www.mypage.com/info#load-stuff;
}
然后在加载目标页面时,评估该哈希信号的URL检查。
Then on load of the target page, evaluate the URL checking for that hash signal.
function pageLoad() {
if (window.location.hash === "#load-stuff") {
$('.my_class').load('my/url/path/with/content/to/load');
}
}
如果您的应用程序使用的是jQuery,它会看起来像什么喜欢:
If your application is using jQuery it'd look something like:
$(function () {
if (window.location.hash === "#load-stuff") {
$('.my_class').load('my/url/path/with/content/to/load');
}
});
这至少是个粗略的想法。
That's the rough idea at least.