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 badges2 Answers
Reset to default 7If 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条)