Search code examples
javascriptjquerywordpressinline-codereflow

How can I avoid this inline Javascript?


I have a wordpress site. Under single.php, I have the following body tag

  <body <?php body_class(); ?>  onLoad="func(<?php echo $thePostID?>);" >

Reading articles on the web made me convinced of avoiding inline CSS and inline javascipt. So I made a restructuring of my site so that styles and scripts are contained now in external files. Except for this line of code since it really need the post id and I dont know how can I retrieve it outside of single.php.

Your usual help is appreciated.


Solution

  • Use attributes:

    <body data-post-id="<?php echo $thePostID?>">
    

    You can then write

    var postId = document.body.getAttribute('data-post-id');