You can use window.location.
window.location.pathname //returns the path (only);
window.location.href //returns the full URL; and
window.location.hostname //returns the domain name of the web host.
Then, you can use it like this:
var url = window.location.hostname + 'new/' + window.location.pathname;
If you want to insert
new
in another part in the URL, you can use
.split(),
.splice() and
.join().
var newUrl = window.location.href.split('/'); //create one array element at every /
newUrl.splice(3, 0, 'new'); //insert 'new' in the position 3
newUrl = newUrl.join('/'); //join every array element with '/' in a string
console.log(newUrl);
Updating the <a>
href
var url = 'mysite.com/index.php?page=post&see=11';
//with javascript
var jsBtn = document.getElementById('jsBtn'); //get the <a> with id 'jsBtn'
jsBtn.setAttribute('href', addNew(url)); //set the attribute 'href' with the new one
//with jQuery
var jqBtn = $('#jqBtn'); //get the <a> with id 'jqBtn'
jqBtn.attr('href', addNew(url)); //change the 'href' attribute with the new url
//function to add 'new' to the array of strings (created with the url string)
function addNew (link){
var newUrl = link.split('/');
newUrl.splice(1, 0, 'new');
newUrl = newUrl.join('/');
return newUrl;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="jsBtn" href="#">javascript anchor</a>
<hr>
<a id="jqBtn" href="#">jQuery anchor</a>