如何在 html 页面上找到涉及 PDF 的链接,然后在链接前面附加绝对路径?

发布于 2025-01-02 11:46:45 字数 278 浏览 0 评论 0原文

我有一个 html 页面,上面有很多链接。一些链接连接到 .pdf。

是否可以编写在加载页面时的 JavaScript -

  • 它扫描 html 文件并找到链接到 .pdf 的所有链接

  • 然后在该 url 的前面附加一个硬编码的前端?

页面上的所有链接都是相对的,当这些 .pdf 链接在 Android 平板电脑上打开时,我遇到了问题。但是当我使用绝对路径时,它处理它没有问题。所以我只想在 .pdf 链接上附加绝对路径。

I have an html page with a number of links on it. Some of the links connect to .pdf.

Is it possible to write javascript that upon the loading of the page -

  • it scans through the html file and locates all links that are linking to .pdf

  • then appends the front of that url with a hard coded front end?

All links on the page are relative, and I am having issues when these .pdf links are pulled up on an android tablet. But when I use the absolute path, it handles it no problem. So I just want to append the absolute path on the .pdf links.

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

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

发布评论

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

评论(2

暖风昔人 2025-01-09 11:46:45

如果您使用 jQuery,这里有一个选择所有这些链接的简单方法:

$('a[href$=".pdf"]').each(function() {
    this.href = 'YOUR URL HERE' + this.href;
});

href 是一个属性选择器,$= 表示它搜索以给定值结尾,即“.pdf”。

如果您不想使用 jQuery,您可以使用标准 JavaScript 来完成,如下所示:

var links = document.getElementsByTagName('a');

for (var i = 0; i < links.length; i++) {
    if (links[i].href.substr(links[i].href.length - 4) == '.pdf') {
        links[i].href = 'YOUR URL HERE' + links[i].href;
    }
}

If you use jQuery, here's an easy way to select all of those links:

$('a[href$=".pdf"]').each(function() {
    this.href = 'YOUR URL HERE' + this.href;
});

The href is an attribute selector, and the $= means that it searches for attributes that end with the given value, namely, ".pdf".

If you don't want to use jQuery, you can do it in standard JavaScript, like so:

var links = document.getElementsByTagName('a');

for (var i = 0; i < links.length; i++) {
    if (links[i].href.substr(links[i].href.length - 4) == '.pdf') {
        links[i].href = 'YOUR URL HERE' + links[i].href;
    }
}
半岛未凉 2025-01-09 11:46:45

与 voithos 相同,但没有 jQuery:

var links=document.links; //Get all links in the document

for (var i=0;i<links.length;i++) { //Loop through each link
    thisHrefExt=links[i].href.split("."); //Split target at all periods
    thisHrefExt=thisHrefExt[thisHrefExt.length-1]; //Select the last section, which should be the extension

    if (thisHrefExt.toLowerCase()=="pdf") { //If the extension is "pdf" (case insensitive)...
        links[i].href="hard-coded-front-end"+links[i].href; //...Add your hard-coded bit at the beginning
    }
}

以函数形式:

function changePDFLinks() {
    var links=document.links; //Get all links in the document

    for (var i=0;i<links.length;i++) { //Loop through each link
        thisHrefExt=links[i].href.split("."); //Split target at all periods
        thisHrefExt=thisHrefExt[thisHrefExt.length-1]; //Select the last section, which should be the extension

        if (thisHrefExt.toLowerCase()=="pdf") { //If the extension is "pdf" (case insensitive)...
            links[i].href="hard-coded-front-end"+links[i].href; //...Add your hard-coded bit at the beginning
        }
    }
}

使用该函数,您可以执行如下操作:

window.onload=changePDFLinks;

这将在页面加载时修复您的 PDF 链接。

The same as voithos, but without jQuery:

var links=document.links; //Get all links in the document

for (var i=0;i<links.length;i++) { //Loop through each link
    thisHrefExt=links[i].href.split("."); //Split target at all periods
    thisHrefExt=thisHrefExt[thisHrefExt.length-1]; //Select the last section, which should be the extension

    if (thisHrefExt.toLowerCase()=="pdf") { //If the extension is "pdf" (case insensitive)...
        links[i].href="hard-coded-front-end"+links[i].href; //...Add your hard-coded bit at the beginning
    }
}

In function form:

function changePDFLinks() {
    var links=document.links; //Get all links in the document

    for (var i=0;i<links.length;i++) { //Loop through each link
        thisHrefExt=links[i].href.split("."); //Split target at all periods
        thisHrefExt=thisHrefExt[thisHrefExt.length-1]; //Select the last section, which should be the extension

        if (thisHrefExt.toLowerCase()=="pdf") { //If the extension is "pdf" (case insensitive)...
            links[i].href="hard-coded-front-end"+links[i].href; //...Add your hard-coded bit at the beginning
        }
    }
}

With the function, you can do something like this:

window.onload=changePDFLinks;

Which will fix your PDF links when the page loads.

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