简体   繁体   中英

Javascript function call in PHP before <head> tag

I have a webpage that uses PHP that must be output before any code is called. In another part of my PHP I need to call a JavaScript function for displaying a message on certain user actions. Is there a way I can call my JavaScript function at the top of my page? I'd rather keep all the PHP together as currently it looks like this;

<?php
     ...Code that must be called first...
?>
...<head></head>....
<?php
     if (PHPFunction()){
         echo "<script>MyJSFunction();</script>";
     }
?>

To me it looks messy having it this way. How can I avoid this?

EDIT:

To make my intentions more clear. The JavaScript function I'm using will display a pop up message, giving the user feedback on actions they are performing. For example, if they upload a file, if move_uploaded_file() is successful, this message gets called. The browser complained when I had my bottom code in the top part saying that my JavaScript function was not defined. When I moved it under it was all good, apart from now looking messy.

Are you using jQuery ? if you are, have you try this code:

<?
     move_uploaded_file(source, destination);
     echo "<script>
        $(document).ready(function () {
        alert ('file successfullu  uploaded');
        });
        </script>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM