2

I have popover with data attributes in every element

Here is snippet

$("[data-toggle=popover]").popover({
  html: true,
  content: function() {
    var content = $(this).attr("data-popover-content");
    return $(content).children(".popover-body").html();
  },
  title: function() {
    var title = $(this).attr("data-popover-content");
    return $(title).children(".popover-heading").html();
  }
});

$('.mainnav').on('click', '.popover-content a', function() { // change this
  let lang = $(this).data("lang")
  console.log(lang);
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<nav class="mainnav">
  <ul class="topnav">
    <a href="#" data-toggle="popover" data-popover-content="#a1">Popover</a>
  </ul>
</nav>
<div id="a1" class="hidden">
  <div class="popover-heading">_('Välj ditt språk')</div>
  <div class="popover-body">
    <div>
      <a href="#" data-lang="no">Norsk</a>
    </div>
    <div>
      <a href="#" data-lang="de">Deutsch</a>
    </div>
    <div>
      <a href="#" data-lang="nl">Nederlands</a>
    </div>
    <div>
      <a href="#" data-lang="es">Español</a>
    </div>
    <div>
      <a href="#" data-lang="dk">Dansk</a>
    </div>
  </div>
</div>

I need to get data-lang attribute and change the domain of the website. for example, it was http://local.test.dk:3000/# and if I choose Norsk, I need to redirect to http://local.test.no:3000/#

Getting data attribute is done, and how I can change link correctly?

Thank's for help.

Udara Abeythilake
  • 1,215
  • 1
  • 20
  • 31
Eugene Sukh
  • 2,357
  • 4
  • 42
  • 86
  • Do you want them to be able to use the back button to return to where they were? Or is this a sort of "permanent" setting that is stored in a cookie for them? – dmgig Jul 09 '18 at 14:07
  • https://stackoverflow.com/questions/1865837/whats-the-difference-between-window-location-and-window-location-replace – dmgig Jul 09 '18 at 14:07
  • I think just redirect them and this is all @dgig – Eugene Sukh Jul 09 '18 at 14:09
  • I think this is all you need then: https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage – dmgig Jul 09 '18 at 14:10
  • there is just redirecting, I need to grab adress and change .dk to .no and etc. @dgig – Eugene Sukh Jul 09 '18 at 14:11
  • Ah, you want to get the url: https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript - then you'd have to do the replace with a regex (or maybe even just a string splice). I will see if I can find some code. – dmgig Jul 09 '18 at 14:14

1 Answers1

3

This is probably not a great regex, but it does the trick:

// test regex
var langCode = 'no';
var curUrl = window.location.href;
var newUrl = curUrl.replace(/(http:\/\/local.test.)dk(:3000\/#)/, '$1'+langCode+'$2');
window.location.replace(newUrl);

Here is a fiddle so you can sort of see it work. Obviously you can't use the url there.

https://jsfiddle.net/49whrtg3/6/

dmgig
  • 4,400
  • 5
  • 36
  • 47