简体   繁体   English

如何在JavaScript中搜索没有https协议的链接

[英]How to search for links without the https protocol in JavaScript

I need to write some JavaScript which identifies any links within that page that aren't using the https protocol and only have http. 我需要编写一些JavaScript来识别该页面中所有未使用https协议且仅包含http的链接。 Once it has found them it needs to change the font of the link to yellow, or if the link is an image, give it a yellow border. 找到它们后,需要将链接的字体更改为黄色,或者,如果链接是图像,请给其添加黄色边框。 I've written some code which searches through the page and collects the a tags, but I'm not sure how to get it to specify only the links which do NOT use https. 我已经写了一些代码来搜索页面并收集a标记,但是我不确定如何获取它以仅指定不使用https的链接。 I've tried using the .protocol on the href but I can tell this is wrong (I'm still learning!) I'm guessing regex would be a good place to start but I don't have the faintest idea how to search for it. 我已经尝试过在href上使用.protocol,但是我可以断定这是错误的(我还在学习!)我猜想regex是一个不错的起点,但是我不知道如何搜索为了它。 Would anyone be able to advise? 有人可以提供建议吗?

Here is the link to my jsfiddle: https://jsfiddle.net/rebeccasmith1301/k87oujgy/ 这是我的jsfiddle的链接: https ://jsfiddle.net/rebeccasmith1301/k87oujgy/

 function find_link_by_href(){ var articleName; // Create variable articleName links = document.getElementsByTagName("a"); // Collect all of the a tags on the page alert(links.length); var counter = 0; // Create a counter and set it to 0 for(var i = 0; i < links.length; i++) { // Crawl through all all links alert(links[i].href.protocol); if( links[i].href.protocol != "https:") { counter = counter + 1; links[i].style.color = "yellow"; links[i].style.border = "solid"; links[i].style.border = "2px"; links[i].style.border = "yellow"; } } alert("There is " + counter + " amount of http links on this page.") } find_link_by_href(); 
 <article> <h1>Hello I am h1</h1> <p>Lorum ipsum lorum ipsum lorum ipsum lorum ipsum</p> <p>Lorum ipsum lorum ipsum lorum ipsum lorum ipsum</p> <p>Lorum ipsum lorum <a href="http://www.google.co.uk">ipsum lorum</a> ipsum lorum ipsum</p> <p>Lorum ipsum lorum ipsum lorum ipsum lorum ipsum</p> <p>Lorum ipsum lorum ipsum lorum ipsum lorum ipsum</p> <p>Lorum ipsum lorum ipsum lorum ipsum lorum ipsum</p> <p>Lorum ipsum lorum ipsum lorum ipsum lorum ipsum</p> <p>Lorum ipsum lorum <a href="https://www.google.co.uk">ipsum lorum</a> ipsum lorum ipsum</p> </article> 

There is no need to use a reg exp, just check to see if the protocol of the link is http. 无需使用reg exp,只需检查链接的协议是否为http。 Also you can just add a class to make it cleaner. 您也可以只添加一个类使其变得更整洁。

 var anchors = document.querySelectorAll("a"); for (var i=0; i<anchors.length; i++) { var anchor = anchors[i]; anchor.classList.toggle("warn", anchors[i].protocol!=="https:"); } 
 .warn { background-color: yellow } 
 <article> <h1>Hello I am h1</h1> <p>Lorum ipsum lorum ipsum lorum ipsum lorum ipsum</p> <p>Lorum ipsum lorum ipsum lorum ipsum lorum ipsum</p> <p>Lorum ipsum lorum <a href="http://www.google.co.uk">ipsum lorum</a> ipsum lorum ipsum</p> <p>Lorum ipsum lorum ipsum lorum ipsum lorum ipsum</p> <p>Lorum ipsum lorum ipsum lorum ipsum lorum ipsum</p> <p>Lorum ipsum lorum ipsum lorum ipsum lorum ipsum</p> <p>Lorum ipsum lorum ipsum lorum ipsum lorum ipsum</p> <p>Lorum ipsum lorum <a href="https://www.google.co.uk">ipsum lorum</a> ipsum lorum ipsum</p> </article> 

Try replacing 尝试更换

links[i].href.protocol

with

links[i].protocol

Here's an updated Fiddle: https://jsfiddle.net/0kc2qxug/ and some additional information regarding protocol : http://www.w3schools.com/jsref/prop_anchor_protocol.asp 这是更新的小提琴: https : //jsfiddle.net/0kc2qxug/和一些有关protocol其他信息: http : //www.w3schools.com/jsref/prop_anchor_protocol.asp

Why use JavaScript? 为什么要使用JavaScript? CSS can do it for you: CSS可以为您做到:

img[src^='http://'] {border:1px solid yellow}
a[href^='http://'] {color:yellow}

You can do that with css 您可以使用CSS做到这一点

a[href^='http://'] {
  color: yellow;
}

a[href^='http://'] > img {
  border: 1px solid yellow;
}

Example : http://jsbin.com/vinayuvaru/edit?html,css,output 范例: http : //jsbin.com/vinayuvaru/edit?html,css,output

Just parse the href: 只需解析href:

function find_link_by_href(){
            var articleName; // Create variable articleName
            links = document.getElementsByTagName("a"); // Collect all of the a tags on the page
            var counter = 0; // Create a counter and set it to 0
            for(var i = 0; i < links.length; i++) { // Crawl through all all links
            var href = links[i].href;
    var host = links[i].host;
    var protocol = href.replace(host, '');
    protocol = protocol.replace('///', '');

      if( protocol !== "https:") { 
                    counter = counter + 1;
                    links[i].style.color = "yellow";
                    links[i].style.border = "solid";
                    links[i].style.border = "2px";
                    links[i].style.border = "yellow";
                } 
            }
            alert("There is " + counter + " amount of http links on this page.")
        }
  find_link_by_href();

https://jsfiddle.net/k87oujgy/1/ https://jsfiddle.net/k87oujgy/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM