Dynamically generated javascript



  • Is there a potential good reason to do this?

    After filling out a form on a website, I got taken to an empty page. The source was

    [code]<html>[/code]
    [code]<script language="Javascript">[/code]


    if (3 == -1) {
    do a thing}


    if (3 == 0) {
    do another thing}


    if (3 == 1) {
    do another thing}


    if (3 == 2) {
    do another thing}

    [code]</script>[/code]

    [code]</html>[/code]

    I'm not so much boggling at the bug as trying to picture why a .asp or something would need to do this tiny bit of logic client-side.



  • Well, duh! Obviously they want to know if 3 == -1 at the client, not at the server. :)



  •  The fools! They're obviously not Jimi Hendrix fans, or they'd have written:

    if (6 == 9) {
            alert("I don't mind.");
    }



  •  Somebody needs to go read a book on localization.  The entire world doesn't live in America, you know.

    Nah, I unno.  Maybe they're using some mythical templating language that doesn't support conditionals or something whacky?

    Actually... Maybe their IDE just doesn't support whatever type of templates they use very well, so the developer keeps it as close to JS as he can.  We use nVelocity and Visual Studio at my office and I've done some cheats like that.  Most of our generated Javascript files start with:

    [code]//for syntax hilighting: <script>[/code]

    Because I set Visual Studio to look at *.vm files as HTML, but we have a few that generate pure JS...

    </script>



  •  seen this a million times.  He's basically trying to say that this website will work just so long as the laws of mathematics don't change.



  • Dynamically generated javascript...

    This reminds me, I once looked at the code of a Firefox addon (can't remember which - this was Firefox 1.0.x), and I saw something like this:

    // Make OtherAddon compatible with ThisAddon

    OtherAddon.SomeFunc = new Function( OtherAddon.SomeFunc.toString().replace('their code', 'our code') );

    Fun stuff.



  • I'm guessing the original code looked more like this...



    <html> <script language="Javascript">

    if (<%=ServerVariable%>== -1) { do a thing}

    if (<%=ServerVariable%> == 0) { do another thing}

    if (<%=ServerVariable%> == 1) { do another thing}

    if (<%=ServerVariable%> == 2) { do another thing}

    </script>

    </html>



    This is a pretty common method for passing server side variables into local script. The code here may be changing the state of buttons, showing or hiding DIVs, etc.



  • @jreasons68 said:

    I'm guessing the original code looked more like this...

    We know.  That's why it was posted here.

     

    @jreasons68 said:

    This is a pretty common method for passing server side variables into local script.

    It's a very bad method, whether or not it is common.

     

    @jreasons68 said:

    The code here may be changing the state of buttons, showing or hiding DIVs, etc.

    Which is what JS is usually used for.  There's no reason most of that couldn't be done with server-generated CSS or HTML or at the very least a server-side script that outputs the appropriate JS instead of just sending all potential JS to the client and doing the work there.  It's definitely a WTF.


Log in to reply