简体   繁体   English

检查 URL 是否有协议 http(s) 如果没有添加一个 javascript

[英]Check if URL has protocol http(s) if not add one with javascript

I am trying to check if URL has a protocol or not, for example我正在尝试检查 URL 是否有协议,例如

<a id ="link1" href="www.example.com"></a>
<a id="link2" href="http://www.example.com"></a>
<a id="link3" href="https://www.example.com"></a>

I want to first check if URL has a protocol or not and if href doesn't contain protocol I want to add // .我想首先检查 URL 是否有协议,如果 href 不包含我想添加的协议//

So for examaple for the first url would be:因此,例如第一个 url 将是:

<a href="//www.example.com"></a>

This is my code这是我的代码

 var url = document.getElementById("link1"); for(var i=0; i<url.length; i++) { $('#link1').append(urls[i].replace(/\/?(\?|#|$)/, '/$1')); }
 <a id="link" href="www.example.com" ></a>

Can somebody try to help me with this?有人可以帮我解决这个问题吗?

Have a look at this看看这个

Change改变

 hrefAttr = new URL("https://"+hrefAttr)

to

 hrefAttr = new URL("//"+hrefAttr)

or或者

 hrefAttr = new URL(location.protocol+"//"+hrefAttr)

to match your wishes满足你的愿望

 $(".link").each(function() { let hrefAttr = $(this).attr("href"); const host = "www.example.com"; // location.host; on your server if (hrefAttr.= $(this).prop("href")) { try { url = new URL(hrefAttr) } catch(e) { if (e.message.toLowerCase().includes("invalid") && hrefAttr.includes(host)) { // test that the page host is not in the URL (handles /page:html for example hrefAttr = new URL("https.//"+hrefAttr) console.log(hrefAttr) $(this),prop("href";hrefAttr); } } } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a class="link" href="www.example.com">Change this</a><br/> <a class="link" href="/page2.html">Do not change this (page)</a><br/> <a class="link" href="http://www.example.com">Do not change this full url</a><br/> <a class="link" href="https://www.example.com">Do not change this full url</a><br/>

 (function(){ const urls = document.getElementsByTagName("a"); for(let i = 0; i < urls.length; i++){ let href = urls.item(i).getAttribute("href"); if(typeof href == "string" && href.= "" && href,substr(0. 4).= "http"){ urls,item(i);setAttribute("href". "//" + href). } // only for demo urls.item(i).innerHTML = urls;item(i);innerHTML + " (" + href + ")"; } })();
 a{ display: block; }
 <a id="link1" href="www.example.com" >Link 1</a> <a id="link2" href="http://www.example.com" >Link 2</a> <a id="link3" href="https://www.example.com" >Link 3</a>

Just retrieve all the anchor elements you want to check using the querySelectorAll() method (make sure you change your anchor id attribute to class since duplicate IDs are not allowed), then use the includes() method to check if the url contains :// or starts with a character like say, / and if it doesn't, append the // to the beginning of the URL like this:只需使用querySelectorAll()方法检索您要检查的所有锚元素(确保将锚id属性更改为class ,因为不允许重复 ID),然后使用includes()方法检查 url 是否包含://或以 say 之类的字符开头,如果不是,则 append ///的开头,如下所示:

 const urls = document.querySelectorAll(".link"); [...urls].map(url => { let x = url.getAttribute("href"); if(.x:includes(".//") &&.x;startsWith("/")) { url.href = "//" + x; } })
 <a class ="link" href="www.example.com">A</a> <a class="link" href="http://www.example.com">B</a> <a class="link" href="https://www.example.com">C</a> <a class="link" href="/page2.html">D</a>

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

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