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 12 of 12
  1. #1
    Regular Coder
    Join Date
    Aug 2012
    Posts
    121
    Thanks
    18
    Thanked 0 Times in 0 Posts

    How to call external js function in php

    I have a js file name js_source.js with the function textDisplay() in it.
    How can I call this fuction by echo of php?
    I try

    Code:
    <?php
       echo "<script src='script/js_source.js'>textDisplay();</script>";
    ?>
    But it doesn't work

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That would indicate that wouldn't work when typed directly into HTML either.
    The problem isn't PHP related, its JS related. PHP cannot "call" a JS function, it can only present what's needed to the client to do the work. In PHP, JS is simply a string and nothing more.
    I don't know if you can use a src attributed script tag with body issuing the call. That would be something that the JS guys could tell you.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You would need two separate <script></script> occurances, one to include your Javascript file, and one to call the Javascript function. Be sure to specify Javascript as your script type, such as <script type='text/Javascript' src='yourscript.js'></script>

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,922
    Thanks
    15
    Thanked 228 Times in 228 Posts
    This is what Fumigator is suggesting:
    PHP Code:
    <?php
       
    echo "<script type='text/javascript' src='script/js_source.js'></script>\n";

       echo 
    "<script type='text/javascript'>textDisplay();</script>\n";
    ?>
    The way you had it isn't 'kosher' for javascript.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This has always confused me about the JS source tag (glad I "guessed" that one right ). Why is it that the tag isn't single sided, but doesn't accept anything in the body? Is there a legal child that you can place in the script container when it has a source attribute?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,922
    Thanks
    15
    Thanked 228 Times in 228 Posts
    Quote Originally Posted by Fou-Lu View Post
    This has always confused me about the JS source tag (glad I "guessed" that one right ). Why is it that the tag isn't single sided, but doesn't accept anything in the body? Is there a legal child that you can place in the script container when it has a source attribute?
    Yeah, the ECMA hasn't updated the method of including code in a page since its beginning. Sort of outdated, but they have to cater to old browsers.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yeah, it just seems like something that should be enforced by the w3. No body allowed? Single side the tag then. Course I don't know how they could deal with the entire script being used for both, but I'd suggest an <import> would be sufficient for all external imports. Attributes: type, src. All done

    Wait, does that still apply to new HTML5? Is <br/> the correct HTML 5 tag, or does it take a step back (in time :P) to the old school <br>? I would *assume* that single sided tags are still single sided.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,922
    Thanks
    15
    Thanked 228 Times in 228 Posts
    Quote Originally Posted by Fou-Lu View Post
    Yeah, it just seems like something that should be enforced by the w3. No body allowed? Single side the tag then. Course I don't know how they could deal with the entire script being used for both, but I'd suggest an <import> would be sufficient for all external imports. Attributes: type, src. All done

    Wait, does that still apply to new HTML5? Is <br/> the correct HTML 5 tag, or does it take a step back (in time :P) to the old school <br>? I would *assume* that single sided tags are still single sided.
    As far as I know it applies to HTML 5, which I'm using all the way on my site. When the browser hits that script tag it turns everything over to the javascript parser, and no new way of doing it has been added.

    Your idea of of a universal <import> or @import method sounds really good to me..
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #9
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Quote Originally Posted by Fou-Lu View Post
    Yeah, it just seems like something that should be enforced by the w3. No body allowed? Single side the tag then. Course I don't know how they could deal with the entire script being used for both, but I'd suggest an <import> would be sufficient for all external imports. Attributes: type, src. All done
    That would suggest Javascript is an actual language, though. We can't have that.

  • #10
    Regular Coder
    Join Date
    Aug 2012
    Posts
    121
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    You would need two separate <script></script> occurances, one to include your Javascript file, and one to call the Javascript function. Be sure to specify Javascript as your script type, such as <script type='text/Javascript' src='yourscript.js'></script>
    Separate the tag is not the key of the problem.
    Instead of an external js file, if you copy the js file to it, it works!!!

    The problem seems that php can not call a js function in an external file

  • #11
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by docco View Post
    The problem seems that php can not call a js function in an external file
    that’s because PHP cannot run JS at all. so there is also no point in calling a JS function. (and even if you use PHP’s JS extensions, there would be the HTML missing, that most JS scripts use).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #12
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The problem seems that php can not call a js function in an external file
    Exactly. You must keep in mind the way PHP works. It builds your entire HTML page on the server side, then pops it over to the client when it's done. It is not interactive with the client. You can output Javascript code, but you can't interact with it.

    But, you can output a call to a JS function from PHP, and as long as the result is valid Javascript, then it will work. But you have to "view source" on the output from PHP to evaluate the Javascript and determine if that same Javascript would work if you manually coded it into an HTML file.


  •  

    Posting Permissions

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