Created
February 2, 2018 20:26
-
-
Save chaimleib/60f972a9a0eabbac5e607edcdf511eb2 to your computer and use it in GitHub Desktop.
trim hostname from fully-qualified URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>Test</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> | |
</head> | |
<body> | |
<div class="main"> | |
hello | |
</div> | |
<script> | |
if (!String.prototype.startsWith) { | |
String.prototype.startsWith = function(searchString, position){ | |
return this.substr(position || 0, searchString.length) === searchString; | |
}; | |
} | |
function relativeUrl(absUrl) { | |
// use fake <a href> to parse urls, since IE doesn't have URL objects | |
var parser = document.createElement('a'); | |
parser.href = absUrl; | |
var rel = parser.pathname + parser.search + parser.hash; | |
if (rel.startsWith("/")) { | |
return rel; | |
} | |
// on IE, there is no leading /, and we need it | |
return "/" + rel; | |
} | |
var $ = jQuery; | |
$().ready(function() { | |
var url = relativeUrl("https://evernote.com/rel/part?search#frag"); | |
alert(url); | |
$(".main").text(url); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment