-1

As an example, I have this code that is able to add the title of a page as a parameter to a link that is identified as an ID. I would like to be able to do the same for the language of the page. Thanks for your help!

<script>
const linkIds = [
  'Your_link_id'
];
linkIds.forEach(id => {
  const interval = setInterval(() => {    
    const link = document.querySelector('#' + id);
    if (link) {
      clearInterval(interval);
      const href = link.getAttribute('href');
      const pageTitle = 
        document
          .title
          .replace(/\s+/g, '_')
          .replace(/&/g, '')
          .toLowerCase()
      const newHref = `${href}?your_parameter_name=${pageTitle}`;
      link.setAttribute('href', newHref);
    }
  }, 20);
});
</script>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

2 Answers2

0

To get language of the page you have to get lang property from the document element. I created an example code which gets and sets the language of the page. You can save the output to some variable and create a link with it.

let lang = document.documentElement.lang

console.log(lang)
// output: en

document.documentElement.lang = 'af'

console.log(document.documentElement.lang)

// output: af
sanitizedUser
  • 1,723
  • 3
  • 18
  • 33
-1

I added this as a new answer because I am not sure what I am suppose to do (this fact itself shows that this is not very well written question) but I will try.

So your script uses some array of IDs. Then it creates an interval for each ID that constantly updates the href attribute of element with that ID with some value. I don't know why would you do that as pageTitle or language is not changing over time but I might just stop asking so here is the edited code:

<script>

    const linkIds = [
        'Your_link_id'
    ];

    // here I get the lanugage of the page
    let lang = document.documentElement.lang;

    linkIds.forEach(id => {
        const interval = setInterval(() => {    
            const link = document.querySelector('#' + id);
            if (link) {
                clearInterval(interval);
                const href = link.getAttribute('href');

                // here I set the language where page title was before 
                const newHref = `${href}?your_parameter_name=${lang}`;

                link.setAttribute('href', newHref);
            }
        }, 20);
    });

</script>

Try it and report back. If you need to use such a complex script, it might do you good to actually learn Javascript.

sanitizedUser
  • 1,723
  • 3
  • 18
  • 33