在动态内容中有效使用 Hashbang、JavaScript、PHP 和 AJAX

发布于 2024-12-12 19:03:44 字数 1065 浏览 3 评论 0原文

我一直在为我开始开发的桌面应用程序构建一个网页,我真的很想利用 HTML5 技术和 JavaScript 的动态内容。基本上,如果用户在浏览器中访问“根”页面(index.php,或只是目录名称),信息、关于、常见问题解答和联系表单部分都将可见(联系表单除外;单击其部分的标题将触发 jQuery 动画来显示它)。页面左侧有一个基本导航,其中包含主页、关于、常见问题解答和联系方式的链接。这些链接的锚点分别是“#!/home”、“#!/about”、“#!/faq”和“#!/contact”。单击其中之一时,JavaScript/jQuery 将隐藏除页眉、页脚和 hashbang 所属部分之外的所有元素(对于联系表单,它还将删除该部分页眉的单击处理程序,因为它当它是页面的唯一内容时,不需要也不应该被隐藏)。目前我正在使用这个:

window.onload = (function() {
    switch(location.hash) {
    case "#!/contact":
        $("body > *:not(section#contact, nav, header, footer)").hide();
        $("#contact > #labels, #contact > form").slideDown('slow');
    // and similar for the other hashbang URIs
    }

    if (location.hash == "#!/" || location.hash == "#!/home" || !eval(location.hash)) {
        $("a[href='#!/contact']").click(function() {
            $("#contact > #labels, #contact > form").slideDown('slow');
        });
    }
    // similar for the other hashbangs
});

我几乎可以肯定有更好的方法来做到这一点,而且我还想确保此页面及其动态内容可以被 Google 机器人正确抓取。我应该使用 PHP 来正确执行此操作吗?为何如此?

谢谢!

I've been putting together a web page for a desktop application I've started developing, and I'd really like to take advantage of HTML5 technologies and dynamic content with JavaScript. Basically, if one visits the "root" page (index.php, or just the directory name) in their browser, sections for info, about, FAQs, and a contact form will all be visible (except for the contact form; clicking on the heading of its section will trigger a jQuery animation to reveal it). On the left side of the page I have a basic navigation with links to home, about, FAQs, and contact. The anchors for these links are "#!/home", "#!/about", "#!/faq" and "#!/contact" (respectively). When one of these is clicked, JavaScript/jQuery will hide all elements save for the header, footer, and the section to which the hashbang pertains (for the contact form, it will also remove the click handler for the header of the section because it doesn't need to and shouldn't be able to be hidden when it is the sole content of the page). Currently I am using this:

window.onload = (function() {
    switch(location.hash) {
    case "#!/contact":
        $("body > *:not(section#contact, nav, header, footer)").hide();
        $("#contact > #labels, #contact > form").slideDown('slow');
    // and similar for the other hashbang URIs
    }

    if (location.hash == "#!/" || location.hash == "#!/home" || !eval(location.hash)) {
        $("a[href='#!/contact']").click(function() {
            $("#contact > #labels, #contact > form").slideDown('slow');
        });
    }
    // similar for the other hashbangs
});

I'm almost certain there is a better way to do this, and I would also like to make sure that this page and its dynamic content are properly crawlable by Google's bots. Should I be using PHP to do this properly? How so?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤带着心事 2024-12-19 19:03:44

Chris Coyier 的 CSS 技巧视频教程一些实施此类事情的最佳实践。要点是确保您的应用程序在打开或不打开 JavaScript 的情况下都可以运行。

This video tutorial on CSS Tricks by Chris Coyier goes over some best practice to implement something like this. A main point is to make sure your application will work with or without javascript turned on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文