I need to write some JavaScript which identifies any links within that page that aren’t using the https protocol and only have 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. 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. Would anyone be able to advise?
Here is the link to my 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>