How to detect the url on webpage using javascript? [duplicate]
I am writing a application using JQuery in which I included my script in the head section of the webpage and the data of the page is开发者_如何学JAVA dynamic and comes from the database.
Now I need to detect the URLs on the page. It can be an anchor tag or a simply a written URL.
For detecting an URL I am using this function
function checkURL(value) {
var urlregex = new RegExp(
"^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){0,1}([0-9A-Za-z]+\.)");
if(urlregex.test(value))
{
return(true);
}
return(false);
}
It works fine but if a URL doesn't have a www
prefix then it fails e.g. myweb.com
Please suggest a better regex for my code which works fine with all valid URLs including sub domains.
Can you also suggest a best way to detect URLs on my webpage?
All hyperlinks will be starting with any of , or etc. Considering this, just call document.getElementsByTagName() to an array. The array will hold all the link contents. Just extract the hyperlink, either direct or from attributes of the array object.
精彩评论