Is it possible to call a javascript function using php?

Discussion in 'PHP' started by Jen-, Sep 18, 2006.

  1. #1
    Could I call on a javascript function using php? Thanks bunches! Jen
     
    Jen-, Sep 18, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    How do you mean call it? Do you mean using ajax? or before the page loads?
     
    mad4, Sep 18, 2006 IP
  3. Mrblogs

    Mrblogs Peon

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Could you give more details as to what you want to do?
     
    Mrblogs, Sep 18, 2006 IP
  4. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #4
    You can use a javascript function to call a PHP script... lookup AJAX
     
    drewbe121212, Sep 18, 2006 IP
  5. tangtang

    tangtang Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you want to do something like this <? javascriptFunction() ?>, then no, because javascript is browser-side.
     
    tangtang, Sep 18, 2006 IP
  6. pcoptimized

    pcoptimized Peon

    Messages:
    193
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You can call javascript functions using php. Use "echo"

    One example...
    <?
    echo 'onclick="return javascriptfunction()..."';
    ?>
    Code (markup):
    From this example you can see that you can do many things to call functions.

    If it's more complex than that, you can also include your javascript files within the php page just like you can with html pages.

    "echo" works.
     
    pcoptimized, Sep 18, 2006 IP
  7. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yes PCO that is what I meant, thanks too cool. I'll try it. Regarding the ...
    dots after the function, I only need the function, I don't really need that do I? Also, do I still need to call the external javascript file at the top of the page with the script tags? Or can I just have it inside the same directory or something?
     
    Jen-, Sep 18, 2006 IP
  8. pcoptimized

    pcoptimized Peon

    Messages:
    193
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    No problem. You're correct, you don't need the dots. I put them there for illustration.
    Basically, you'll call the javascript file however you wish. If you do it in the php code (inside the <? ) then you'll need to write a line like:
    echo "<script language=...>";
    But since you can go in and out of html coding on php pages, you could just put your javascript call to the external javascript file within your html pages as you normally would and when you bounce back into your php code on that page, as long as you use echo it will act the same as if you were only coding a html page.

    You main issue that may arise is getting the quotations correct. But it works. I go in and out of javascript all the time in my php files.

    If you find issues with it not working for you, post a little snippet of your code and we'll see if we can't get it working right. :)
     
    pcoptimized, Sep 18, 2006 IP
  9. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #9
    Yeah just take normal HTML, and make sure to Escape ALL quotes
     
    drewbe121212, Sep 18, 2006 IP
  10. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hmmm thanks a lot, still a bit confused. Just one reason of concern is I have one javascript that is mine that I would like to protect, but not my only reason for this original question.

    Regarding the external js protection though, any way to call on the external js with php, without revealing its external .js address? Appreciated! Jen
     
    Jen-, Sep 19, 2006 IP
  11. pcoptimized

    pcoptimized Peon

    Messages:
    193
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I'm sorry, I don't know if there is a way to protect a call to a javascript file.
    You might try using the php function include(). But you would still have to echo out the code to the browser as once said before, javascript is browser/client side processed.
    If you have a javascript that you're wanting to protect, I, myself am not sure of a way to do. Sorry.
     
    pcoptimized, Sep 19, 2006 IP
  12. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Why not just rewrite the javascript as a php file and call it via ajax. That way nobody can steal your code.
     
    mad4, Sep 19, 2006 IP
  13. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Interesting thought but do you mean I have to rewrite all the javascript into php code instead? Thank ya!! :)
     
    Jen-, Sep 19, 2006 IP
  14. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #14
    You would do if you want to call it with ajax.
     
    mad4, Sep 21, 2006 IP
  15. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    So I can call php and javascript with ajax?
     
    Jen-, Sep 21, 2006 IP
  16. jhinga-la-la Queen

    jhinga-la-la Queen Peon

    Messages:
    79
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #16
    yes.. Ajax is in general speaking - "Javascript on steroids"..

    In ajax you can make server calls using Javascript+XML httprequest.

    Alternatively you can consider encrypting javascript to hide the code. (its not very effective though, but can save a lot of pain...)
     
    jhinga-la-la Queen, Sep 21, 2006 IP
  17. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #17
    drewbe121212, Sep 22, 2006 IP
  18. webdesignperth

    webdesignperth Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    hello.
    if you mean "calling a javascript using php file"? then yes, it is possible. just include the <script> tag (similar to calling css). hope this helps

    <SCRIPT language="JavaScript" SRC="http://www.yourdomain.com/jscript/xyz.js"></SCRIPT>
     
    webdesignperth, Nov 3, 2010 IP