Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to detect external URLs

What's the fastest method to detect if foo='http://john.doe' is an external url (in comparsion to window.location.href)?

like image 573
mate64 Avatar asked Jun 04 '11 17:06

mate64


People also ask

How do I find external links on my website?

To find external links to your website, you'll need to use an SEO tool such as Google Search Console or Moz's Link Explorer. These indexes can show you known backlinks to your site, along with other information such as the anchor text used.

What is external URL?

External links are a hyperlinks that point at (target) any domain other than the domain the link exists on (source). External links have a greater impact on search engine rankings than internal links because they are valued by search engines as external votes of confidence/popularity in a web page.

What are examples of external links?

Overview. An external link is a link that goes to any website outside of Cascade Server. In other words, any website that does not start with "www.csueastbay.edu". Examples of external links include: Google, East Bay Today, and Microsoft.


1 Answers

If you consider a URL being external if either the scheme, host or port is different, you could do something like this:

function isExternal(url) {     var match = url.match(/^([^:\/?#]+:)?(?:\/\/([^\/?#]*))?([^?#]+)?(\?[^#]*)?(#.*)?/);     if (typeof match[1] === "string" && match[1].length > 0 && match[1].toLowerCase() !== location.protocol) return true;     if (typeof match[2] === "string" && match[2].length > 0 && match[2].replace(new RegExp(":("+{"http:":80,"https:":443}[location.protocol]+")?$"), "") !== location.host) return true;     return false; } 
like image 151
Gumbo Avatar answered Sep 19 '22 14:09

Gumbo