-4

How can I get existing URL and use as link in jQuery, example?

If my browser is on page mysite.com/index.php?page=post&see=11 I want use URL in my link and add /new/ after domain name like this :

<a href="mysite.com/new/index.php?page=post&see=11">link text</a>
mmvsbg
  • 3,570
  • 17
  • 52
  • 73
arkonasi
  • 1
  • 1
  • 1
    http://stackoverflow.com/help/how-to-ask - We're here to help, not to write code for you. Show us what you've tried so far and we'll help you find your mistakes. But literally nobody will write code for you. Without you showing us any effort to solve your problem, we won't do so neither. – Twinfriends Feb 06 '18 at 12:17
  • i need it for jquery – arkonasi Feb 06 '18 at 12:19
  • This will help you [window.location...](https://www.w3schools.com/js/js_window_location.asp) you could get the URL as a string and work with it... – DIEGO CARRASCAL Feb 06 '18 at 12:19
  • DIEGO CARRASCAL it get full url and how can i change it – arkonasi Feb 06 '18 at 12:21
  • What do you mean by "how can i change it"? Your question tells nothing about changing the URL to anything else – Nico Haase Feb 06 '18 at 12:24
  • Adding "new/" to the string. Ones I didn't know also how to work with strings... I recommend you (@arkonasi) to read about these things, the web that I linked you is very simple and you could learn these basic things from it. good look. – DIEGO CARRASCAL Feb 06 '18 at 12:30

2 Answers2

0

This will help you window.location... you could get the URL as a string and work with it. Find ".com/" within the string, split it and then concatenate the two half with "new/" in the middle. After that, you can use it to create the link or navigate to it... or do

<a id="newLink" href="#" id="demo">my link</a> 
<script> 
  $(document).ready(function(){
   var newURL = window.location.hostname.toString();
   newURL = newURL + "new/";
   newURL = newURL + window.location.pathname.toString(); 
   $("#newLink").attr("href", newURL);  
  });
</script>
DIEGO CARRASCAL
  • 1,999
  • 14
  • 16
0

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>
KL_
  • 1,503
  • 1
  • 8
  • 13