function changeUrlEnd(url, newEnd) {
// Create an HTML anchor element to parse the URL
var parser = document.createElement('a');
parser.href = url;
// Modify the path with the new end
var path = parser.pathname.replace(/\/$/, '') + '/' + newEnd;
// Rebuild the URL with the modified path
var modifiedUrl = parser.protocol + '//' + parser.hostname + path;
// Add query string and fragment if present
if (parser.search) {
modifiedUrl += parser.search;
}
if (parser.hash) {
modifiedUrl += parser.hash;
}
return modifiedUrl;
}
// Example usage
var originalUrl = "https://example.com/some/path/page.html";
var newEnd = "new-page.html";
var modifiedUrl = changeUrlEnd(originalUrl, newEnd);
console.log("Original URL:", originalUrl);
console.log("Modified URL:", modifiedUrl);