Skip to content

Instantly share code, notes, and snippets.

@phloe
Created January 9, 2012 14:17
Show Gist options
  • Select an option

  • Save phloe/1583118 to your computer and use it in GitHub Desktop.

Select an option

Save phloe/1583118 to your computer and use it in GitHub Desktop.
Crossdomain url test
/*
document.location of the current page tested against a url (string) like:
"pages/page.html" // false
"../pages/page.html" // false
"/pages/page.html" // false
"//foo.bar.com/pages/page.html" // false
"http://foo.bar.com/pages/page.html" // false
"//bar.com/pages/page.html" // true
"https://bar.com/pages/page.html" // true
*/
function isXDomain (url) {
return !(
url.match(/^((\.\.\/)|([^:/]+\/)|(\/[^/]))/) ||
(url.match(/^\/\//) || url.indexOf(location.protocol) == 0) &&
url.indexOf("//" + location.hostname) > -1
)
}
@dperini
Copy link

dperini commented Jan 10, 2012

Haven't checked about the validity of that URL, however try changing the RE to:

var HOSTNAME = /\/\/([^\/]+)\//;

to avoid that dubious condition and see if it fits exactly all your tests/needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment