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

 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="Сохранить в избранное браузера"','"http://www.odnoklassniki.ru/dk?st.cmd=addShare&st._surl='+u+'&title='+t+'" title="Добавить в Одноклассники"','"#" onclick="window.open(\'http://vkontakte.ru/share.php?url='+u+'\', \'_blank\', \'scrollbars=0, resizable=1, menubar=0, left=200, top=200, width=554, height=421, toolbar=0, status=0\');return false" title="Поделиться В Контакте"','"http://connect.mail.ru/share?url='+u+'&title='+t+'" title="Поделиться в Моем Мире@Mail.Ru"','"http://www.facebook.com/sharer.php?u='+u+'&t='+t+'" title="Поделиться в Facebook"','"http://twitter.com/share?text='+t+'&url='+u+'" title="Добавить в Twitter"','"http://www.livejournal.com/update.bml?event='+u+'&subject='+t+'" title="Опубликовать в LiveJournal"','"http://www.linkedin.com/shareArticle?mini=true&url='+u+'&title='+t+'" title="Добавить в Linkedin"','"http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk='+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){p.style.top=m1-top+'px';}else{p.style.top=m2+'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 title= " Save to browser favorites ", it is in place of " Save to browser favorites " that you need to insert that variable from the php file. Better yet, insert the value of the abbreviated code right away by an example.

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

Thanks in advance for your reply!

  • Everything. Understood - YurikFirst

2 answers 2

Alternatively, you can write the value in a hidden field using PHP, and when you run the JS script, read its value in the JS variable.

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

If not mistaken, then so.

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

or so.

  • Cleverly said, but by example? - YurikFirst
  • Like a good option. - Palmervan
  • Added in response. - DemoS
  • And how, sorry, to insert this code into a solid javascript code? There I wrote in the question at the very bottom where exactly to insert .. nothing happens there var value = document.forms.myform.myval.value; or var value = document.getElementByName ("myval"). value; - YurikFirst
  • Try to take an element not through your name, but through an Idi to use the operator var value = document.getElementById ("myval"). value; - deivan_
  1. On the php page:

    echo '<script type = "text / javascript"> var MY_VAR =' .json_encode ($ my_var). '; </ script> ';

  2. Use ajax and request the necessary data.

  3. already proposed option with input (you can also add to the hidden div)
  • But about the last two points can be on the example of the code? thanks - YurikFirst