简体   繁体   English

如何在AJAX页面导航中使用锚的href

[英]How to use href of an anchor with AJAX page navigation

I have the following problem: I'm trying to create a HTML5 page based on pure AJAX page navigation. 我遇到以下问题:我试图基于纯AJAX页面导航创建HTML5页面。 So I have on main page called "index.html". 所以我在主页上有“ index.html”。 This one holds my css, scripts and the menu. 这个包含我的CSS,脚本和菜单。 The menu looks like this: 菜单如下所示:

<ul>
    <li><a id="startLink" class="navlink" href="pages/start.html">Start</a></li>
    <li><a class="navlink" href="pages/test.html">Test</a></li>
</ul>

By clicking on one of this anchors I call a JavaScript loading the page under "href" with an AJAX call into a div on "index.html". 通过单击其中一个锚点,我调用JavaScript,通过AJAX调用将“ href”下的页面加载到“ index.html”上的div中。 So my main page never reloads and all the new content is just displayed there in a container. 因此,我的主页永远不会重新加载,所有新内容仅显示在容器中。 The function looks like this: 该函数如下所示:

$(document).ready(function()
{
var popped = ("state" in window.history && window.history.state !== null), initialURL = location.href;

$(".navlink").click(function(e)
{
    var link    = $(this);
    var href    = link.attr("href");

    loadContent(href);
    window.history.pushState(null, null, href);

    e.preventDefault();
});

$(window).bind("popstate", function(e)
{
    // Ignore inital popstate that some browsers fire on page load
    var initialPop = !popped && location.href == initialURL;
    popped = true;
    if (initialPop) return;

    loadContent(location.pathname);
});

init();
});

function init()
{
    $("#startLink").click();
};

function loadContent(url)
{
    $("main").load(url);
};

I'm using the history.pushState and popstate functions to deal with the browser history and display URLs in the Browser. 我正在使用history.pushState和popstate函数来处理浏览器历史记录并在浏览器中显示URL。 This just works fine, but now we are coming to my problem. 这很好,但是现在我们来解决我的问题。

When I load the index.html, my init() function clicks on the start anchor to display some initial content on the page. 当我加载index.html时,我的init()函数单击开始锚点以在页面上显示一些初始内容。 Well, now, if I want to go to test.html the browser shows me the path like "Host/Somepath/pages/pages/test.html". 好吧,现在,如果我要转到test.html,浏览器将向我显示“ Host / Somepath / pages / pages / test.html”之类的路径。 Same with start: .../pages/pages/start.html. 与开始相同:... / pages / pages / start.html。 I guess the reason are my href on the anchors because the browser thinks he has entered my folder pages and shows start.html. 我想原因是锚点上的我的href,因为浏览器认为他已进入我的文件夹页面并显示start.html。 From there he wants to navigate to pages/pages/ to go to any other page in my menu. 他想从那里导航到页面/页面/以转到我菜单中的任何其他页面。 My project structure looks kind of this: 我的项目结构看起来像这样:

  • index.html index.html
  • css 的CSS
  • images 图片
  • scripts 剧本
  • pages 页数
    • start.html start.html
    • test.html test.html

Has anyone an idea how i can deal with this issue? 有谁知道我该如何处理这个问题? I dont want to place all htmls in one folder, i need subfolders to structure the project a bit. 我不想将所有html放在一个文件夹中,我需要子文件夹来对项目进行结构化。

Try with HTML5 localStorage or sessionStorage 尝试使用HTML5 localStoragesessionStorage

Sample Code 样例代码

window.sessionStorage.setItem('isFirst', true);
$(document).ready(function () {

  if(window.sessionStorage.getItem('isFirst')){
    window.sessionStorage.setItem('isFirst', false);
    //Put your code here. It will work only once
    init();
  }    
});

You can read more about HTML local & session store click 您可以阅读有关HTML本地和会话存储点击的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM