“Как добавить JavaScript в php” Ответ

Как добавить JavaScript в php

//this is my code 

you can add javascript to your code by using the echo statement and rap all 
your javascript between "" and if there is "" in the javascript code
then add \ before any "", and for the php inside rap it with ''
  
  
<?php
  echo "<script>document.write(\"hello world\")</script>"
?>
  
//other example

<?php
  $a = 5;
  echo "<script>document.write('$a')</script>"
?>

//other example
  
<?php 
$TokenData = Auth::user();

echo "<script type=\"text/javascript\">
             localStorage.setItem(\"id\", '$TokenData->id');
             localStorage.setItem(\"name\", '$TokenData->name');
             localStorage.setItem(\"email\", '$TokenData->email');
      </script> ";
Wide-eyed Wryneck

Как включить JavaScript в PHP

// simply echo the path anywehre in php you need

echo "<script src="script.js" type="text/javascript"></script>"
Himanshu Jangid

Добавить javaScript в functions.php

/****
  To get started, locate and open your functions.php file, 
  then copy and paste the following code snippet:
****/
function ti_custom_javascript() {
    ?>
        <script>
          // your javscript code goes here
        </script>
    <?php
}
add_action('wp_head', 'ti_custom_javascript');

/****
This code above will add JavaScript to your header. 
To apply it to a single post only, you can use the following:
****/
function ti_custom_javascript() {
  if (is_single ('3')) { 
    ?>
        <script type="text/javascript">
          // your javscript code goes here
        </script>
    <?php
  }
}
add_action('wp_head', 'ti_custom_javascript');
/****
Note that you’ll need to replace the 3 
in the above example with the post ID number 
to which you want to add the code. To locate 
that number, navigate to the post from your 
admin dashboard, and then click on Edit. In 
the browser bar’s URL, the ID number is the 
number next to “post=”:
CoderHomie

Ответы похожие на “Как добавить JavaScript в php”

Вопросы похожие на “Как добавить JavaScript в php”

Больше похожих ответов на “Как добавить JavaScript в php” по PHP

Смотреть популярные ответы по языку

Смотреть другие языки программирования