Problem using html as script src (working example inside)

Discussion in 'JavaScript' started by Logisti, Nov 22, 2007.

  1. #1
    I apologize if this is something really basic and obvious I am missing but I am practically tearing my hair out over this.

    This site metroguardian.net has a script call that uses this url as the source:

    www.metroguardian.net/shop/cart.php?target=special_offer&action=show_box

    (so in other words, it works when I do this: <script src="http://www.metroguardian.net/shop/cart.php?target=special_offer&amp;action=show_box"></script> )

    and that url appears to be regular HTML. I am trying to do something similar where a PHP script will generate a dynamic piece of HTML and then I can include it in a static page by calling it like this <script src="http://www.mysite.com/example.php?generate"></script> but it just doesn't work. What is metroguardian.net doing that they are able to serve that page up as a script?
     
    Logisti, Nov 22, 2007 IP
  2. godkillah

    godkillah Guest

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you can even use a .exe file to as script src,
    however the outputs have to be only js without script tags,

    lets say i got <script src='file.php'></script>

    and i wanted to write the IP addres using js,
    the content of file.php should be
    <?php
    echo 'document.write("'.$_SERVER['REMOTE_ADDR'].'")';
    ?>
    PHP:
    actually just the same as when using a .js file :)

    -Godkillah
     
    godkillah, Jan 6, 2008 IP
  3. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    In that case you would have to send the correct headers (text/javascript).
     
    MMJ, Jan 6, 2008 IP
  4. Webray

    Webray Active Member

    Messages:
    469
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Include this line as the first line after <?php

    Header("content-type: application/x-javascript");

    That will tell the Browser to output the PHP script results as a JAVA.


    Your output will need to use the document.write method..., and there should be only one call. So, gather up everything before displaying.

    EX:
    // display output
    echo "document.write(\"". addslashes($my_output) . "\")";


    ...hope that helps.
     
    Webray, Jan 6, 2008 IP
    bogart likes this.
  5. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    @webray: although application/javascript is the correct mime type IE doesn't seem to support it. :(

    @OP: What did you try? Do you have any code?
     
    MMJ, Jan 6, 2008 IP
  6. Webray

    Webray Active Member

    Messages:
    469
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #6
    You're using the wrong call, it's application/x-javascript and it works for me.
     
    Webray, Jan 24, 2008 IP
  7. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Oops.

    But the correct mime-type is application/javascript.

     
    MMJ, Jan 24, 2008 IP