能否使内部链接在客户端生成的 HTML 页面中起作用?

发布于 2024-12-06 18:29:34 字数 695 浏览 1 评论 0原文

情况:由于各种原因(主要是它有时会在互联网不可用的情况下使用),我正在构建的一些 JavaScript 重的 HTML 有时必须能够严格在客户端上运行,而不涉及服务器。在大多数情况下,我已经能够想出解决方法,允许通过常用浏览器的“页面另存为”机制保存此站点的页面,嵌入他们需要的所有部分,并使用按摩路径来引用我想要的位置当浏览器不够智能,无法修复 URL 时(在本地计算机上)(这种情况比我想象的要频繁)。

不过,我还无法解决一个问题:每个主页都可以在新选项卡/窗口中打开帮助页面。为此,我将帮助页面的内容嵌入到保存的主页面中。然后我可以使用 helpWindow.document.write(helpContent) 打开它。问题:就浏览器而言,帮助页面最终的 URL 与原始页面相同,因此我无法有效地使用该页面上的页面内部链接:如果您单击,它会尝试加载已保存的主页面一!

例如:帮助中的 ... link页面:如果单击“链接”,浏览器将加载保存的主页面,而不是滚动帮助页面。

我的临时解决方法是当我必须在这种环境中操作时删除这些链接,但我肯定有办法让它们工作。有什么建议吗?建议可能包括完全不同的打开帮助页面的方式。不过,我不想使用 iframe,我真的希望它保留在单独的选项卡/窗口中。

Situation: for various reasons (mainly, that it will be used at times in situations where the Internet is not availble), some JavaScript-heavy HTML I am building has to be able to run at times strictly on the client, with no server involved. For the most part, I've been able to come up with workarounds that allow pages from this site to be saved by the usual browser 'Save Page As' mechanisms, embed all the pieces they need, and massage paths to refer where I want them to (on the local machine) when the browser isn't smart enough to fix the URL (which is more often than I might have thought).

One piece I haven't been able to solve yet, though: each main page can open a help page in a new tab/window. To do that, I embed the content of the help page in the main saved page. Then I can use helpWindow.document.write(helpContent) to open that. Problem: as far as the browser is concerned, the help page ends up with the same URL as the original page, so I can't effectively use page-internal links on that page: it tries to load the main saved page if you click one!

For example: <a name="target" /> ... <a href="#target">link</a> in the help page: if you click "link", the browser loads the main saved page, rather scrolling the help page.

My temporary workaround is to strip these links when I have to operate in this environment, but I'd sure rather have a way to make them work. Any suggestions? Suggestions could include an entirely different way to open a help page. I'd rather not use iframes, though, I'd really like it to stay in a separate tab/window.

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

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

发布评论

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

评论(2

纵性 2024-12-13 18:29:35

您可以通过 JavaScript 使用 element.scrollIntoView() 滚动到书签:

function goToBookmark(e)
{
    e = e || window.event;
    if (e.preventDefault)
    {
        e.preventDefault();
    }
    e.returnValue = false;
    var bookmarkName = this.href.replace(/^#/, "");
    var bookmark = document.getElementsByName(bookmarkName)[0];
    if (bookmark)
    {
        bookmark.scrollIntoView();
    }
}
for (var i = 0; i < document.links.length; i++)
{
    var link = document.links[i];
    if (/^#/.test(link.href))
    {
        link.onclick = goToBookmark;
    }
}

或者,如果您使用的是 jQuery:

$("a[href^='#']").click(function(e) {
    e.preventDefault();
    var bookmark = $("a[name=" + this.href.replace(/^#/, "") + "]")[0];
    if (bookmark) {
        bookmark.scrollIntoView();
    }
});

You can scroll to the bookmark with JavaScript with element.scrollIntoView():

function goToBookmark(e)
{
    e = e || window.event;
    if (e.preventDefault)
    {
        e.preventDefault();
    }
    e.returnValue = false;
    var bookmarkName = this.href.replace(/^#/, "");
    var bookmark = document.getElementsByName(bookmarkName)[0];
    if (bookmark)
    {
        bookmark.scrollIntoView();
    }
}
for (var i = 0; i < document.links.length; i++)
{
    var link = document.links[i];
    if (/^#/.test(link.href))
    {
        link.onclick = goToBookmark;
    }
}

Or, if you are using jQuery:

$("a[href^='#']").click(function(e) {
    e.preventDefault();
    var bookmark = $("a[name=" + this.href.replace(/^#/, "") + "]")[0];
    if (bookmark) {
        bookmark.scrollIntoView();
    }
});
一个人的夜不怕黑 2024-12-13 18:29:35

您应该阅读以下内容:http://en.wikipedia.org/wiki/Single-page_application

特别尝试:http://en.wikipedia.org/wiki/TiddlyWiki

(抱歉,那这并不能回答你的问题,但如果你没有看到这些,也许你可以从他们那里得到想法。)

编辑:

我尝试了一下,这真的很奇怪!对我来说这几乎就像一个错误。所有浏览器都这样做吗?

解决方案可能是使用 ID 而不是 A NAME (顺便说一句,即使您想通过锚片段链接,您也可以/应该这样做),然后使用

document.getElementById(elID).scrollIntoView();

To 跳转到元素。

You should read this: http://en.wikipedia.org/wiki/Single-page_application

In particular try: http://en.wikipedia.org/wiki/TiddlyWiki

(Sorry, that this doesn't answer your question, but if you have not see those, maybe you can get ideas from them.)

Edit:

I tried this out a bit, and that's really strange! It almost seems like a bug to me. Do all browsers do this?

A solution could be to use ID instead of A NAME (which BTW you can/should do even if you wanted to link by anchor fragment) and then use

document.getElementById(elID).scrollIntoView();

To jump to the element.

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