对jQuery $ .get和$ .load感到困惑

时间:2022-08-24 21:02:35

On my site, I would like to implement AJAX page loading, as seen on Facebook, Twitter, and the comparatively smaller site Kotaku Gaming.

在我的网站上,我想实现AJAX页面加载,如Facebook,Twitter和相对较小的网站Kotaku Gaming所见。

Basically, this is what I want to do: have a header that remains static throughout all pages on the domain, and only load content into a specific container, using AJAX and jQuery.

基本上,这就是我想要做的事情:在域中的所有页面上保持一个静态的头,并且只使用AJAX和jQuery将内容加载到特定的容器中。

jQuery's $.load() is almost perfect— but only almost. This is basically the code I'd use:

jQuery的$ .load()几乎是完美的 - 但差不多。这基本上就是我使用的代码:

$("#content").load("site.html");

However, when another page is "loaded" with this function, the URL does not change. This will ruin bookmarking as well as linking, and is therefore unacceptable. The URL needs to change.

但是,当使用此功能“加载”另一个页面时,URL不会更改。这将破坏书签和链接,因此是不可接受的。 URL需要更改。

The problem with $.get() is that the header does change along with the rest of the page, which of course is not what I'm looking for.

$ .get()的问题在于标题确实随页面的其余部分而改变,这当然不是我正在寻找的。

Can anybody please help me accomplish this? If the sites mentioned above can do it, why can't I?

有人可以帮我解决这个问题吗?如果上面提到的网站可以做到,为什么我不能?

2 个解决方案

#1


1  

The first answer on How can I change the page URL without refreshing the page? should do the trick for you - using pushstate to push the new address. Note this won't work in older browsers, I'd suggest the best theory is to fall back to traditional methods then.

关于如何在不刷新页面的情况下更改页面URL的第一个答案?应该为你做的 - 使用pushstate推送新地址。请注意,这在旧浏览器中不起作用,我建议最好的理论是回归传统方法。

#2


0  

If you want the URL to change with each page load but want the header to stay the same, it may be worth just creating a template for the header and then changing the content section of each page.

如果您希望URL随每个页面加载而更改但希望标题保持不变,则可能只需为标题创建模板,然后更改每个页面的内容部分。

If you are using something link PHP, this should be pretty simple with an include/require or similar.

如果你使用链接PHP的东西,这应该是非常简单的包含/要求或类似。

#1


1  

The first answer on How can I change the page URL without refreshing the page? should do the trick for you - using pushstate to push the new address. Note this won't work in older browsers, I'd suggest the best theory is to fall back to traditional methods then.

关于如何在不刷新页面的情况下更改页面URL的第一个答案?应该为你做的 - 使用pushstate推送新地址。请注意,这在旧浏览器中不起作用,我建议最好的理论是回归传统方法。

#2


0  

If you want the URL to change with each page load but want the header to stay the same, it may be worth just creating a template for the header and then changing the content section of each page.

如果您希望URL随每个页面加载而更改但希望标题保持不变,则可能只需为标题创建模板,然后更改每个页面的内容部分。

If you are using something link PHP, this should be pretty simple with an include/require or similar.

如果你使用链接PHP的东西,这应该是非常简单的包含/要求或类似。