Skip to content

Instantly share code, notes, and snippets.

@chaimleib
Created February 2, 2018 20:26
Show Gist options
  • Save chaimleib/60f972a9a0eabbac5e607edcdf511eb2 to your computer and use it in GitHub Desktop.
Save chaimleib/60f972a9a0eabbac5e607edcdf511eb2 to your computer and use it in GitHub Desktop.
trim hostname from fully-qualified URL
<!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