Prepare a subfolder for all links using jQuery

I am currently working on a site in a staging environment located at www.example.com/staging. All links for the site are relative (for example: / about /). I am trying to write some kind of simple jQuery to add "/ staging" to every link on the pages, so I don’t need to change all the links for a week or so, it is in an intermediate environment. I know that an HTML tag is an option, but because links are added dynamically using ExpressionEngine, which doesn't seem to work. I think jQuery should be pretty simple. Here is what I have, but something is not quite right:

$(function() {
  var href = $('a').attr('href');
  $(href).prepend('/staging');
});

Thanks in advance for your help.

+3
source share
4 answers

Try the following:

$(function() {
  $('a').each(function() {
    var href = $(this).attr('href');
    $(this).attr('href', '/staging' + href);
  });
});
+8
source
$(function() {
  $('a').each(function() {
    $(this).attr('href', '/staging' + $(this).attr('href'));
  });
});

$(href) does not find anything, because it is simply an attribute value.

+2
source

URL- (/about/ /staging) ; . , .

URL ; ( ). , .

+1

jQuery <base>.

<base href="http://www.example.com/staging/" /> <head> /about/ ( ..) , .

, , . , URL-, about/, . /about/ . , - Google .

URL- ./about/, . ( ) URL ./about/ http://www.example.com/about/ <base> http://www.example.com/staging/about/, <base> .

0

Source: https://habr.com/ru/post/1771351/


All Articles