好的,大家好,我是 Stack Overflow 的新手(就注册而言),但经常使用它来寻找解决方案。我已经搜索了我的问题,虽然我找到了几篇几乎与我的问题相关的文章和问题,但没有一个真正回答我的确切问题。如果这个问题被问了一百万次,我们深表歉意!
我对 JQuery 比较陌生,但一般对 JavaScript 有一些经验。我对我试图在我的新网站上实施的 history.pushstate 和 history.popstate 方法完全陌生。我没有使用 ajax 加载内容,而是将其加载到 iframe 中,但同样的原则适用,我需要相应地更新页面 URL 并能够在浏览器中返回和转发。
请在你的回答中对我温柔,虽然我相当称职,你会从我的代码中看到我并不过分高级!
好的,我已经成功实现了 pushstate,并且页面加载似乎工作正常(示例在这里... http://beta.casafondaradio.com)但是当我单击浏览器后退按钮时,它没有所需影响。即似乎我必须单击它两次才能触发popstate 功能。我无法弄清楚我做错了什么。
主要功能是通过相关链接上的 onclick 属性触发的。我知道有一种更优雅的方法可以做到这一点,但我只是想让它对我来说简单易读!
我的链接(在 ASP.NET 中)的格式如下:
<li runat="server" data="home" id="liHome" class="menuList"><asp:HyperLink ID="hlHome" onclick="return pageLoader(this);" data="home" CssClass="menuLink" runat="server" ToolTip="Home | CasafondaRadio.com" NavigateUrl="~/"><i class="fa fa-home" aria-hidden="true"></i> HOME</asp:HyperLink></li>
<li runat="server" data="schedule" id="liSched" class="menuList"><asp:HyperLink ID="hlSched" onclick="return pageLoader(this);" data="schedule" CssClass="menuLink" runat="server" ToolTip="Schedule | CasafondaRadio.com" NavigateUrl="~/schedule.aspx"><i class="fa fa-clock-o" aria-hidden="true"></i> SCHEDULE</asp:HyperLink></li>
<li runat="server" data="events" id="liEvent" class="menuList"><asp:HyperLink ID="hlEvent" onclick="return pageLoader(this);" data="events" CssClass="menuLink" runat="server" ToolTip="Events | CasafondaRadio.com" NavigateUrl="~/events.aspx"><i class="fa fa-star" aria-hidden="true"></i> EVENTS</asp:HyperLink></li>
<li runat="server" data="residentdjs" id="liDJs" class="menuList"><asp:HyperLink ID="hlDJs" onclick="return pageLoader(this);" data="residentdjs" CssClass="menuLink" runat="server" ToolTip="Resident DJs | CasafondaRadio.com" NavigateUrl="~/residentdjs.aspx"><i class="fa fa-headphones" aria-hidden="true"></i> DJS</asp:HyperLink></li>
<li runat="server" data="about" id="liAbout" class="menuList"><asp:HyperLink ID="hlAbout" onclick="return pageLoader(this);" data="about" CssClass="menuLink" runat="server" ToolTip="About | CasafondaRadio.com" NavigateUrl="~/about.aspx"><i class="fa fa-info-circle" aria-hidden="true"></i> ABOUT</asp:HyperLink></li>
<li runat="server" data="contact" id="liCont" class="menuList"><asp:HyperLink ID="hlCont" onclick="return pageLoader(this);" data="contact" CssClass="menuLink" runat="server" ToolTip="Contact | CasafondaRadio.com" NavigateUrl="~/contact.aspx"><i class="fa fa-envelope" aria-hidden="true"></i> CONTACT</asp:HyperLink></li>
<li runat="server" data="webplayers" id="liPlay" class="menuList"><asp:HyperLink ID="hlPlay" onclick="return pageLoader(this);" data="webplayers" CssClass="menuLink" runat="server" ToolTip="Web Players | CasafondaRadio.com" NavigateUrl="~/webplayers.aspx"><i class="fa fa-cog" aria-hidden="true"></i> PLAYERS</asp:HyperLink></li>
<iframe id="contentFrame" frameborder="0" style="height:100px;" scrolling="no" src='<asp:Literal ID="mainFrameSource" runat="server"></asp:Literal>'></iframe>
我在头部的 Javascript 代码是......
<script type="text/javascript">
function resetiFrame(obj) {
$('#contentFrame').css("height", "100px");
}
function resizeiFrame(obj) {
if ($('#contentFrame').attr('src') != '') {
var dochgt = $(obj.contentWindow.document).height();
$('#contentFrame').css("height", dochgt + "px");
}
}
// Page Loader Functions
var pageUrl = "", pageTitle = "", pageRef = "", pageContent = "",
lastPageUrl = "", lastPageTitle = "", lastPageRef = "", lastPageContent = "";
init = function() {
// Do this when a page loads.
// Page initialisation stuff will go here
};
function pageLoader(objLink) {
var data = objLink.getAttribute('data'),
titText = objLink.getAttribute('title'),
url = objLink.getAttribute('href');
// alert('data = ' + data + '\ntitle = ' + titText + '\nurl = ' + url);
lastPageUrl = pageUrl; lastPageTitle = pageTitle; lastPageRef = pageRef; lastPageContent = pageContent;
pageUrl = url; pageRef = data; pageTitle = titText; pageContent = data + "Content.aspx";
historyUpdate(pageRef, pageTitle, pageUrl);
loadPageData(pageContent);
addActiveClass(data);
return false;
}
function historyUpdate(data, title, url) {
window.history.pushState(data, title, url);
}
function loadPageData(urlToOpen) {
$("#contentFrame").attr('src', urlToOpen);
init();
}
function addActiveClass(data) {
$('#list li').removeClass('active');
$('.menuList').filter('[data="' + data + '"]').addClass('active');
}
</script>
我之前正文关闭标记中的 javascript 代码是...
<script type="text/javascript">
theiFrame = document.getElementById("contentFrame");
$('#contentFrame').load(function () {
resetiFrame(theiFrame);
resizeiFrame(theiFrame);
});
$(window).resize(function () {
resetiFrame(theiFrame);
resizeiFrame(theiFrame);
});
$(window).on("popstate", function (e) {
var origState = e.originalEvent.state;
if (origState !== null) {
// Load previous content
alert(origState);
if (origState == lastPageRef) {
alert("is last Page Ref!");
}
} else {
alert('else : ' + e.originalEvent.state);
}
});
init();
</script>
如前所述,我已将示例上传到上面链接的 beta URL,它处于当前状态(即不工作)。任何帮助将不胜感激,显然我做错了什么,但我不知道它是什么。
提前致谢!
解决!
修改我的loadPageData()
功能如下...
function loadPageData(urlToOpen) {
$('#contentFrame').remove();
var ifr = $('<iframe/>', {
id: 'contentFrame',
src: '/' + urlToOpen,
style: 'height:100px;',
load: function () {
resizeiFrame();
}
});
$('#frameContainer').append(ifr);
}
并在其中添加了一个 IDframeContainer
到带有原始iframe
内容的 DIV !