Activate a js function in a webpage clicking from another webpage

It would be possible activate a js function in a webpage clicking from another webpage?
in a webpage I have this html code, working:

<span onclick="setIt()">ita</span><span onclick="setEs()">esp</span><span onclick="setEn()">eng</span>

calling this js code:


function setEn() {
  document.body.setAttribute("lang", "en"); 
}
function setIt() {
  document.body.setAttribute("lang", "it"); 
}
function setEs() {
  document.body.setAttribute("lang", "es"); 
}

Could I activate, from a link in another webpage, in the target webpage, that function?

If it is possible then you need to use an extension. To use an extension you must spend time learning about them. The following appear to be relevant.

1 Like

Thank you. But I need a solution indipendent from extensions: all the visitors must see the same.

EDIT

Maybe it would be better use the php. Something like here.
But I need three, and not two alternatives (ita, esp, eng) for stylesheets.
Should I open a thread in php section?

Whilst waiting for a possible PHP solution
here is a JavaScript example for you to try…

page-one.html

<!DOCTYPE HTML>
<html lang="en">
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">

<title>Page One</title>

</head>
<body>
 <h1>Page One</h1>
 <ul>
  <li>
   <a href="page-two.html?lang=ita">Italian</a>
  </li><li>
   <a href="page-two.html?lang=esp">Spanish</a>
  </li><li>
   <a href="page-two.html?lang=eng">English</a>
  </li>
 </ul>
</body>
 
</body>
</html>

page-two.html

<!DOCTYPE HTML>
<html lang="en">
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">

<title>Page Two</title>

</head>
<body>

 <h1>Page Two</h1>

<script>
(function( w, d ) {

   const params = new URLSearchParams(w.location.search);
   d.body.setAttribute( 'lang',params.get('lang') ) ;

}( window, document ));
</script>

</body>
</html>
1 Like

Thank you very much. Tomorrow I will try your code.

The question is vague. I assumed you were asking how to click a button in a page for another domain.

Samples as was provided previously typically provide only two instances when it is assumed that it is relatively easy to use the same technique for more.

Please do not assume we understand the abbreviations ita, esp, eng. I think you mean Italian, Spanish and English. Therefore you are probably asking how to provide multiple translations, as in Internationalization | Our mission | W3C. There are hundreds of articles and samples of doing that. There are even entire books.

1 Like

Yes, ita, esp, eng means Italian, Spanish and English.
I have a multilingual website, and I use this trick:

in css


/* --- multilingual BEGIN ---*/
body[lang=it] *[lang]:not([lang="it"]){display:none;}
body[lang=en] *[lang]:not([lang="en"]){display:none;}
body[lang=es] *[lang]:not([lang="es"]){display:none;}
/* --- multilingual BEGIN ---*/

the same page has an html code like the following:

<span lang='it'>Perù</span>
<span lang='es'>Perú</span>
<span lang='en'>Peru</span>

and when I set (via php) the value of body language, I see only the <span> of that language.

So far I had small files such the following (membri-es.php)

<?php 
$lang="es";
$title="Miembros";
$stylespecific="stylespecific.css";
include "mypath/membri.php";
?>

And so I open membri.php (with all the content, in three languages) with Spanish language, and so on.

Your code

Unfortunately your code, as is, doesn’t work for my needs. Sorry.

If you are referring to mine, then it may be because this…

 <ul>
  <li>
   <a href="page-two.html?lang=ita">Italian</a>
  </li><li>
   <a href="page-two.html?lang=esp">Spanish</a>
  </li><li>
   <a href="page-two.html?lang=eng">English</a>
  </li>
 </ul>

…should be…

 <ul>
  <li>
   <a href="page-two.html?lang=it">Italian</a>
  </li><li>
   <a href="page-two.html?lang=es">Spanish</a>
  </li><li>
   <a href="page-two.html?lang=en">English</a>
  </li>
 </ul>

I obviously had a ‘brain fart’ whilst at the keyboard. :wonky:.

1 Like

:slight_smile: Thank you.
But I think is not for that: is your script that makes my whole lang code no more working.

Since what I was searching for don’t appear reachable with js, it would be better if I open a php thread instead?
I suppose that here I can get only js suggestions…

What are you trying to do?
Are the web pages on the same domaine eg www.pageA.domainX.net and www.pageB.domainX.net
Or are they on different domains.
If the former and you are creating them, then there are several methods.
Or are you creating seperate page versions for different languages.

1 Like

Sorry, I forgot to say here that I solved, with php. You can see here.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.