返回按钮从javascript模板加载相关html
我正在为我的应用程序使用JavaScript模板,类似于:返回按钮从javascript模板加载相关html,javascript,jquery,html,templates,Javascript,Jquery,Html,Templates,我正在为我的应用程序使用JavaScript模板,类似于: <script id="my-template" type="text/template"> <!-- Template here --> </script> 根据用户执行的操作,将每个模板的内容加载到#containerdiv中 问题是,这一切都发生在同一个页面上,因此,当用户单击浏览器中的后退和前进按钮时,他们将转到以前的网页,而不是我的应用程序中的相关屏幕。有没有办法说,如果我在加
<script id="my-template" type="text/template">
<!-- Template here -->
</script>
根据用户执行的操作,将每个模板的内容加载到#container
div中
问题是,这一切都发生在同一个页面上,因此,当用户单击浏览器中的后退和前进按钮时,他们将转到以前的网页,而不是我的应用程序中的相关屏幕。有没有办法说,如果我在加载模板时将
#我的模板id
字符串添加到我的url,或者知道要转到哪个页面/要加载哪个模板?您可能想看看您可能想看看是的,您需要在应用程序中使用某种路由器来处理片段给你
以下是fullstack框架中使用的两个示例:
window.location.hash
(排除#)。要查看当前哈希是什么,只需使用window.location.hash
字符串(参见下面的示例)。如果要基于哈希更改视图,也可以监听哈希更改。为此,请使用onhashchange
侦听器。请注意,当您通过window.location.hash
更改哈希时,它会触发HashChange。使用标志可以避免混淆,例如
//making hash change
imChangingHash = true;
window.location.hash = 'thisPage';
$(window).on('hashchange',function(){
if(imChangingHash) {
imChangingHash = false;
return;
}
//this is now a back/forward button hash change
console.log('back - forward button moved page to: ' + window.location.hash);
});
是的,只需使用要添加的哈希设置
window.location.hash
(排除#)。要查看当前哈希是什么,只需使用window.location.hash
字符串(参见下面的示例)。如果要基于哈希更改视图,也可以监听哈希更改。为此,请使用onhashchange
侦听器。请注意,当您通过window.location.hash
更改哈希时,它会触发HashChange。使用标志可以避免混淆,例如
//making hash change
imChangingHash = true;
window.location.hash = 'thisPage';
$(window).on('hashchange',function(){
if(imChangingHash) {
imChangingHash = false;
return;
}
//this is now a back/forward button hash change
console.log('back - forward button moved page to: ' + window.location.hash);
});