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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript and php

    I have an external javascript function :
    function a () { return a;}
    I have a php file :
    how do i use the return value from external javascript in php as i want to dynamically switch base don vlaue

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 594 Times in 593 Posts
    external / internal there is no difference.
    Code:
    function a () { return a;}
    is nonsense. You have to declare 'a' in order to return a.

    JS only talks to php via AJAX. https://developer.mozilla.org/en-US/docs/AJAX

    AND the job of JS is to change the DOM. Why do you want PHP to do this?
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,251
    Thanks
    12
    Thanked 340 Times in 336 Posts
    Quote Originally Posted by sunfighter View Post
    Code:
    function a () { return a;}
    is nonsense. You have to declare 'a' in order to return a.
    but a is declared. it is the reference to the function itself.

    PHP Code:
    function a() {
      
    console.log(1); // do something visible
      
    return a;
    }
    var 
    a(); // 1
    x(); // 1 
    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

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,251
    Thanks
    12
    Thanked 340 Times in 336 Posts
    Quote Originally Posted by sunfighter View Post
    AND the job of JS is to change the DOM. Why do you want PHP to do this?
    to expand on this: JS changes the DOM, PHP creates the HTML source code.
    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

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 594 Times in 593 Posts
    @Dormilich, please explain what your code does. I'm lost
    function a() {
    console.log(1); // do something visible
    return a;
    }
    var x = a(); // 1
    x(); // 1
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,251
    Thanks
    12
    Thanked 340 Times in 336 Posts
    sure.

    function a() {
    define a function named "a"

    console.log(1);
    send "1" to the console

    return a;
    return the Function object we currently define (or rather, a reference to the function object in memory)

    var x = a();
    execute a and assign the return value (the function object a, resp. its reference (since it is an object) to the variable x). this will dump "1" to the console

    x();
    execute function a (the variables x and a point to the same function object). "1" gets dumped to the console. the return value is not saved anywhere.
    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

  • #7
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 594 Times in 593 Posts
    OK, thank you. Then without the console nothing is done in the program. In other words you normally would not have a function return itself, cause it just made no sense to me at all.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,251
    Thanks
    12
    Thanked 340 Times in 336 Posts
    Quote Originally Posted by sunfighter View Post
    In other words you normally would not have a function return itself, cause it just made no sense to me at all.
    I can’t think of a reason right now. but there is lots of use in referring to the current function (e.g. memoizing).
    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

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,622
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by sunfighter View Post
    OK, thank you. Then without the console nothing is done in the program. In other words you normally would not have a function return itself, cause it just made no sense to me at all.
    The console call in that code is there just to represent whatever the function itself is supposed to be doing. It can be anything that you want to do in JavaScript. The only situation where having the function return a pointer to itself is where the function contains no other content. Then it would be returning a pointer to a function that does nothing.
    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.

  • #10
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for all your suggestions

    The thing is i am embedding the External js in my php file and then calling the external js and the reason i am using pure js is cause of performance issue, if i load jquery then i have to load for he whole 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
    •