Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342

    Outputting Javascript from PHP File

    I have a file:

    PHP Code:
    <?php
    ob_start
    ();
    ?>
    <html>
    <head>
    <style type="text/css">
    * {
        font-size:12px;
    }
    ..... some stuff
    </body>
    </html>
    <?php
    $contents 
    ob_get_contents();
    $bads = array("\n","\t","\r");
    $nc str_replace($bads,'',$contents);
    ob_end_clean();
    ?>
    <script type="text/javascript">
    document.write('<?=$nc;?>')
    </script>
    The above would work, if I remove:
    Code:
    <script type="text/javascript">
    </script>
    and add:
    PHP Code:
    header("Content-Type: application/x-javascript"); 
    before ob_start() the entire text+javascript would be outputed as HTML.

    First one works fine, but when I try to add it:

    Code:
    <script type="javascript"
      src="http://localhost/display2.php?lan=english&do=180_60">
    </script>
    it would be blank?

    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  2. #2
    The first file can also be displayed using :
    PHP Code:
    <?php
    ob_start
    ();
    ?>
    <html>
    <head>
    <style type="text/css">
    * {
        font-size:12px;
    }
    ..... some stuff
    </body>
    </html>
    <?php
    $contents 
    ob_get_contents();
    $bads = array("\n","\t","\r");
    $nc str_replace($bads,'',$contents);
    ob_end_clean();
    ?>
    <?php 
    echo $nc?>
    Code:
    <script type="javascript"
      src="http://localhost/display2.php?lan=english&do=180_60">
    </script>
    it would be blank?
    Because it's not javascript but a php file
    try to do it like
    PHP Code:
    <?php include 'http://localhost/display2.php?lan=english&do=180_60'?>
    Hope that helps.

  3. #3
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342
    The file I made is Javascript (with php extension).

    The text displayed also conforms with javascript (tried it by itself).

    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •