Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Using PHP to output a JS code for <script> tag

    Hi,

    This one should be simple I guess. I'm quite newbie with PHP, that's why asking.

    I'd like to build a script that outputs JS code that can be used within a <script> tag.

    For example:

    Code:
    <script type="text/javascript" language="javascript" src="http://mydomain.com/widgetscript.php?v=param1"></script>
    So the JS source is returned from the PHP script, according to the desired parameters.

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Just echo it out like you would with HTML:

    echo('<script type="text/javascript" language="javascript" src="http://mydomain.com/widgetscript.php?v=param1"></script>');

    Then echo out the JS in the PHP file you're src'ing according to taste.

    HTH
    Dan
    Last edited by whizard; 08-09-2007 at 03:29 PM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Dan, thanks.

    yup, it works. Now I have another icky problem.
    I'm echoing rather a big HTML chunk, and apparently it isn't output at all.

    When I truncated it to 4-5 lines, it does get echoed ok.

    So I guess it might be a problem of the string length that is passed to the 'echo' function?

    should I create a string var instead?

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    I'm, a newbie to this to so i may be totaly wrong, But perhaps using the heredoc syntax as suppose to echo will work better in your case. heredocs are good when u have a large amount if text to output. check out http://ca.php.net/types.string and scroll down a bit to where they talk about heredocs.

    *Any php guru's out there don't feel bad to bash my solution if it is wrong. haha

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Simply escaping out of PHP to specify it as plain HTML might be easier.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New Coder
    Join Date
    Aug 2007
    Location
    Wagga, Australia
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post
    instead of echoing a large chunk of items why not do this

    PHP Code:
    <html>
    <head>
    <title>Example</title>
    </head>

    <body>
    <?php
    $num 
    rand(120);
    if(
    $num 10) {
    ?>
    <div id="option1">
    hi this will only appear if $num is greater than 10!
    </div>
    <?php }
    else {
    ?>
    <div id="option2">
    hello, this will only appear if $num is less than or equal to 10
    </div>
    <?php ?>
    </body>
    </html>
    you might find it strange to keep closing and opening php tags, but truth is the page will load slower if you rely on php to echo everything. this way there won't be any problems with echos especially if you have forgotten to backslash something out and the page will load quicker.


  •  

    Posting Permissions

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