FAQ Topic - How do I get a perl/asp/php variable into client-side js? (2008-03-02)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FAQ server

    FAQ Topic - How do I get a perl/asp/php variable into client-side js? (2008-03-02)

    -----------------------------------------------------------------------
    FAQ Topic - How do I get a perl/asp/php variable into
    client-side js?
    -----------------------------------------------------------------------

    Use the server-side language to generate the javascript:

    var jsvar="<%= aspvar %>";
    var jsvar="<?php echo $phpvar ?>";


    --
    Postings such as this are automatically sent once a day. Their
    goal is to answer repeated questions, and to offer the content to
    the community for continuous evaluation/improvement. The complete
    comp.lang.javas cript FAQ is at http://jibbering.com/faq/index.html.
    The FAQ workers are a group of volunteers. The sendings of these
    daily posts are proficiently hosted by http://www.pair.com.

  • toby.oconnell@gmail.com

    #2
    Re: FAQ Topic - How do I get a perl/asp/php variable into client-sidejs? (2008-03-02)

    On Mar 1, 4:00 pm, "FAQ server" <javascr...@dot internet.bewrot e:
    -----------------------------------------------------------------------
    FAQ Topic - How do I get a perl/asp/php variable into
    client-side js?
    -----------------------------------------------------------------------
    >
    Use the server-side language to generate the javascript:
    >
     var jsvar="<%= aspvar %>";
     var jsvar="<?php echo $phpvar ?>";
    >
    --

    This FAQ entry should probably mention that the server-side value (if
    a string) needs to be escaped for whatever style quote is surrounding
    the javascript var statement (double-quote here).

    It should be fairly obvious, but the people asking this question will
    probably be fooled by the simplicity of the two example statements. I
    almost missed this pitfall the first time I wrote such code.


    Comment

    • toby.oconnell@gmail.com

      #3
      Re: FAQ Topic - How do I get a perl/asp/php variable into client-sidejs? (2008-03-02)

      This FAQ entry should probably mention that the server-side value (if
      a string) needs to be escaped for whatever style quote is surrounding
      the javascript var statement (double-quote here).
      >
      It should be fairly obvious, but the people asking this question will
      probably be fooled by the simplicity of the two example statements.  I
      almost missed this pitfall the first time I wrote such code.
      Oh yes, and new lines in a string will break js literals so they need
      to be handled specially. Probably other pitfalls I am not aware of.
      A one sentence note of these pitfalls would be helpful.

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: FAQ Topic - How do I get a perl/asp/php variable into client-sidejs? (2008-03-02)

        toby.oconnell@g mail.com wrote:
        On Mar 1, 4:00 pm, "FAQ server" <javascr...@dot internet.bewrot e:
        >-----------------------------------------------------------------------
        >FAQ Topic - How do I get a perl/asp/php variable into
        >client-side js?
        >-----------------------------------------------------------------------
        >>
        >Use the server-side language to generate the javascript:
        >>
        > var jsvar="<%= aspvar %>";
        > var jsvar="<?php echo $phpvar ?>";
        >[...]
        >
        This FAQ entry should probably mention that the server-side value (if
        a string) needs to be escaped for whatever style quote is surrounding
        the javascript var statement (double-quote here).
        Good catch.

        var jsvar = "<?php echo addslashes($php var); ?>";

        Or with asp_tags=1 in php.ini:

        var jsvar = "<%= addslashes($php var); %>";


        PointedEars
        --
        var bugRiddenCrashP ronePieceOfJunk = (
        navigator.userA gent.indexOf('M SIE 5') != -1
        && navigator.userA gent.indexOf('M ac') != -1
        ) // Plone, register_functi on.js:16

        Comment

        Working...