javascript - How can I locate links involving PDF's on an html page and then append the front of the link with an absolu

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

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.

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.

Share Improve this question asked Feb 3, 2012 at 20:03 Jason MichalakJason Michalak 311 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

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;
    }
}

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744787325a4593705.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信