How do I include a javascript in PHP?

Discussion in 'PHP' started by mnymkr, Sep 5, 2006.

  1. #1
    I am using a joomla module that can be used for custom php and javascript. The module only read PHP so to use a javascript you have to write a php document and inlcude the Javascript in it.

    How do I set this up?
     
    mnymkr, Sep 5, 2006 IP
  2. surefire

    surefire Guest

    Messages:
    40
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not familiar with Joomla, so there may be some special code required by your plugin, but in general, you can write PHP code that outputs regular ol' html to the browser.

    
    echo '<script src="foo.js"></script>';
    
    PHP:
    or

    
    echo '<script>window.location = "foo.html";
    </script>';
    
    PHP:
     
    surefire, Sep 5, 2006 IP
    ahkip likes this.
  3. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #3
    That is correct, you can use php echo to output javascript to the browser.

     
    danielbruzual, Sep 5, 2006 IP
  4. Reuben

    Reuben Peon

    Messages:
    162
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Or, you could just end the php block and continue after like:
    ?>
    <script src="foo.js"></script>
    <?

    As with their way, you might end up with escaping problems.
     
    Reuben, Sep 5, 2006 IP
  5. redbayron2006

    redbayron2006 Active Member

    Messages:
    173
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #5
    The below should do the trick
    <script type'text/javascript' src='sourceofthescript' ></script>
     
    redbayron2006, Sep 5, 2006 IP