最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url - Use JavaScript to find a specific link - Stack Overflow

programmeradmin3浏览0评论

Could anyone help me with a function for JavaScript which searches the source code and compares all of the links on the site with the specific link I am looking for.

For example: I am executing the JavaScript on www.youtube and I am looking for one specific YouTube link.

It could look like this (which of course doesn't work):

if(document.body.indexOf("")!== -1){
    console.log("Url found");
}else{
  console.log("Url not found");

}

How can I accomplish this?

Could anyone help me with a function for JavaScript which searches the source code and compares all of the links on the site with the specific link I am looking for.

For example: I am executing the JavaScript on www.youtube.com and I am looking for one specific YouTube link.

It could look like this (which of course doesn't work):

if(document.body.indexOf("http://www.youtube.com/SPECIFICURL")!== -1){
    console.log("Url found");
}else{
  console.log("Url not found");

}

How can I accomplish this?

Share Improve this question edited Jun 10, 2021 at 19:20 DontVoteMeDown 21.5k10 gold badges71 silver badges113 bronze badges asked Mar 5, 2014 at 16:58 user3361146user3361146 1031 gold badge3 silver badges9 bronze badges 2
  • First off you need to get all the links on the page using document.links then loop through those using the indexOf function. – user2417483 Commented Mar 5, 2014 at 17:04
  • You don't want to search source code. You have a DOM at your disposal. – cookie monster Commented Mar 5, 2014 at 17:06
Add a comment  | 

4 Answers 4

Reset to default 13

Try querySelectorAll() with CSS3 selectors:

document.querySelectorAll('a[href*="http://www.youtube.com/SPECIFICURL"]')

Fiddle

This selector says find all links with an href attribute that contains a specific string. Lets break this down a little bit:

  • a - this is the element type, e.g. "link"
  • href - this is the element attribute
  • *= - this essentially means contains. There are different type of equality checks that can be used: starts with, contains word, ends with, etc. The jQuery selectors documentation is quite good.
  • "..." - everything in quotes after the equal sign is the substring the query is looking for.
function find_link_by_href(address)
    links = document.getElementsByTagName("a");

    for(var i = 0; i < links.length; i++) { 
      if( links[i].href === address ) {
        console.log("Url found"); 
        return; 
     } 
 }

You can call it like this:

find_link_by_href("http://www.youtube.com/SPECIFICURL");

Use Array.prototype.some to see if at least one element in document.links has the href you're looking for.

var has_link = [].some.call(document.links, function(link) {
   return link.href === 'http://www.youtube.com/SPECIFICURL';
});

You can patch it in old browsers using the patch found at MDN Array#some

You can use document.links to get the anchors, then just loop through grabbing the href, like this:

    var arr = [], l = document.links;
    for(var i=0; i<l.length; i++) {
      arr.push(l[i].href);
    }

//arr is now an array of all the href attributes from the anchors in the page

See here.

Then loop through the array to check for your specific link.

发布评论

评论列表(0)

  1. 暂无评论