CodeIgniter: passing variables from .php file -> to file.js

Good day, dear coders.
I want to ask how to pass a $ variable from a .php file -> to a .js file. And that it would be displayed on the page, as in the case of the code in <?php echo $a;?>. After all, php code is ignored in a file with *.js extension, and changing the extension is not an option ((.
I can provide the code for the file.js

function share42(f,u,t,m1,m2){if(!u)u=location.href;if(!t)t=document.title;u=encodeURIComponent(u);t=encodeURIComponent(t);if(!m1)m1=150;if(!m2)m2=20;document.write('<span id="share42">');var s=new Array('"" onclick="return fav(this);" title="   "','"'+u+'&title='+t+'" title="  "','"#" onclick="\''+u+'\', \'_blank\', \'scrollbars=0, resizable=1, menubar=0, left=200, top=200, width=554, height=421, toolbar=0, status=0\');return false" title="  "','"'+u+'&title='+t+'" title="   @Mail.Ru"','"'+u+'&t='+t+'" title="  Facebook"','"'+t+'&url='+u+'" title="  Twitter"','"'+u+'&subject='+t+'" title="  LiveJournal"','"'+u+'&title='+t+'" title="  Linkedin"','"'+u+'&title='+t+'" title="   Google"','"#" onclick="print();return false" title=""','"#" onclick="return up()" title=""');for(i=0;i<s.length;i++)document.write('<a rel="nofollow" style="display:block;width:32px;height:32px;margin:0 0 6px;outline:none;background:url('+f+'icons.png) -'+32*i+'px 0" href='+s[i]+' target="_blank"></a>');document.write('</span>'); window.onload=function(){var p=document.getElementById('share42');function m(){var top=Math.max(document.body.scrollTop,document.documentElement.scrollTop);if(top+m2<m1){'px';}else{'px';}}m();window.onscroll=m;}}function fav(a){title=document.title;url=document.location;try{window.external.AddFavorite(url,title);}catch(e){try{window.sidebar.addPanel(title,url,"");}catch(e){if(typeof(opera)=="object"){a.rel="sidebar";a.title=title;a.url=url;return true;}else{alert(' Ctrl-D,     ');}}}return false;};var t;function up(){var top=Math.max(document.body.scrollTop,document.documentElement.scrollTop);if(top>0){window.scrollBy(0,-200);t=setTimeout('up()',20);}else clearTimeout(t);return false;};

Where the moment is at the very beginning of title=Save to browser favorites“, exactly at the place “Save to browser favorites” you need insert the same variable from the php.
Better yet, immediately insert the value of the shortened code there

<?php echo lang('menu_main');?>

Thanks in advance for your reply!

Answer 1, authority 100%

Alternatively, you can write a value to a hidden field using PHP, and when executing a JS script, read its value into a JS variable.

<form name="myform">
<input type="hidden" name="myval" value="<?echo '$value';?>">
var value = document.forms.myform.myval.value;

If I’m not mistaken, then so.

var value = document.getElementByName("myval").value;

or so.

Answer 2

  1. On the php page:

    echo ‘
    <script type=”text/javascript”>
    var MY_VAR = ‘.json_encode($my_var).’;

  2. Use ajax and request the required data

  3. already suggested option with input (you can also add it to the hidden div)