Replace String in URL using JavaScript
To replace a string in the current page URL using JavaScript, you have to use the replace
function with location.href
as below
var url = location.href.replace('/ar/','/en');
Set <a>
tag link in HTML using JavaScript
To set the new URL to <a>
Link tag in HTML, try to do this
<html>
<head>
<script>
function replaceString()
{
return location.href=location.href.replace('/ar/','/en/') ;
}
</script>
</head
<body>
<a id="language" href="#" onclick="replaceString()">debug</a>
</body>
</html>
Set <a>
tag link in HTML using JQuery
You can also use JQuery to set the href
parameter in <a>
tag as below
$("#language").on('click', function() {
window.location = location.href.replace('/ar/','/en/') ;
});