how to eval in the global scope

Discussion in 'Javascript' started by a.farhadi@gmail.com, Oct 27, 2005.

  1. Guest

    hi guys,

    I want to eval some javascript code at a function. and my code contains
    some variable and function definitions.

    my problem is that the code is evaluated in function scope not global
    scope and definations are not available in the global scope.
     
    , Oct 27, 2005
    #1
    1. Advertising

  2. Randy Webb Guest

    said the following on 10/26/2005 11:07 PM:
    > hi guys,
    >
    > I want to eval some javascript code at a function. and my code contains
    > some variable and function definitions.


    Why do you think you need eval?

    > my problem is that the code is evaluated in function scope not global
    > scope and definations are not available in the global scope.


    Then make them global in scope.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
     
    Randy Webb, Oct 27, 2005
    #2
    1. Advertising

  3. Farhadi Guest

    I load some js code on runtime using XMLHTTPRequest.
    but I cant run it on the global scope.

    Is there any more ways except eval?
     
    Farhadi, Oct 27, 2005
    #3
  4. Farhadi Guest

    Randy Webb wrote:
    > > I load some js code on runtime using XMLHTTPRequest.
    > > but I cant run it on the global scope.
    > > Is there any more ways except eval?

    >
    > That depends on the code itself actually.
    >
    > Have the code generated in a manner that it can be appended in a script
    > block, wrapped in a function, and then the function called. But that
    > depends on the code and what it is supposed to do.
    >
    > There also may be a better way than an XMLHTTPRequest.





    before, I had included my js files thruogh Script block at the HEAD.

    now I want to include them using XMLHTTPRequest because of 2 reason:
    1. I want to load scripts on onload event of body. to speed up loading
    of page.
    2. I dont want my scripts be saveable thruogh "Save As" at browser.

    and I want to run my script just like including them on a script block.
    is there any more ways?
     
    Farhadi, Oct 27, 2005
    #4
  5. RobG Guest

    Farhadi wrote:
    > Randy Webb wrote:
    > > > I load some js code on runtime using XMLHTTPRequest.
    > > > but I cant run it on the global scope.
    > > > Is there any more ways except eval?

    > >
    > > That depends on the code itself actually.
    > >
    > > Have the code generated in a manner that it can be appended in a script
    > > block, wrapped in a function, and then the function called. But that
    > > depends on the code and what it is supposed to do.
    > >
    > > There also may be a better way than an XMLHTTPRequest.

    >
    >
    >
    >
    > before, I had included my js files thruogh Script block at the HEAD.
    >
    > now I want to include them using XMLHTTPRequest because of 2 reason:
    > 1. I want to load scripts on onload event of body. to speed up loading
    > of page.


    Then use an onload event to load the scripts by adding script elements
    with src attributes that link the files.

    But your intention seems misguided, unless the scripts are really huge
    (by script standards, say more than 50KB) then they will have little or
    no effect on the time taken to load the page (assuming a non-trivial
    site).


    > 2. I dont want my scripts be saveable thruogh "Save As" at browser.


    Presuming XMLHttpRequest will prevent that (and even if it does, how
    long will it last?) what is the point? It will not prevent
    knowledgeable users from creating their own local copies of your script
    files should they wish.

    If your scripts are worth copying, anyone defeated by XMLHttpRequest or
    other means will likely not have a use for them anyway. Those who know
    how to get around your defences will - your scripts will be in the
    hands of those who may (if my guessing of your thoughts is correct)
    best benefit from them.

    In my experience such users find it easier and faster to write their
    own scripts, particularly if they are to be used on a site with any
    kind of quality control system.

    >
    > and I want to run my script just like including them on a script block.
    > is there any more ways?


    Then do that. There are utilities to minify code, some of which result
    in sufficient obfuscation that the effort required to de-mystify the
    code makes it not worth while (though I would never suggest that as a
    sole criterion for minification, it may be considered a useful
    side-effect by some).

    <URL:http://www.crockford.com/javascript/jsmin.html>


    --
    Rob
     
    RobG, Oct 27, 2005
    #5
  6. RobG wrote:

    > > 2. I dont want my scripts be saveable thruogh "Save As" at browser.

    >
    > Presuming XMLHttpRequest will prevent that (and even if it does, how
    > long will it last?) what is the point? It will not prevent
    > knowledgeable users from creating their own local copies of your script
    > files should they wish.


    I agree. HTTPRequest certainly won't protect against this, as you can
    use another HTTPRequest in the address bar to get the script quite
    easily.

    E.g.:

    javascript: var sURL="http://www.somewebsite.com/myscript.js"; var
    oRequest=new ActiveXObject("Msxml2.XMLHTTP"); oRequest.open("GET",
    sURL,false); oRequest.send(null); alert(oRequest.responseText);

    >There are utilities to minify code


    This is another one to consider, for compression purposes.

    http://dean.edwards.name/packer/

    Julian
     
    Julian Turner, Oct 27, 2005
    #6
  7. > but I cant run it on the global scope.

    remove the "var" declorations before the variable names and they will
    be global, just make sure they don't conflict with another variable
    with the same name.

    > Please quote what you are replying to.
    >
    > If you want to post a followup via groups.google.com, don't use the
    > "Reply" link at the bottom of the article. Click on "show options" at
    > the top of the article, then click on the "Reply" at the bottom of the
    > article headers.


    Lose the canned message text. If it were important to people other than
    you it would be in the FAQ.
     
    Joshie Surber, Oct 27, 2005
    #7
  8. Randy Webb Guest

    Joshie Surber said the following on 10/27/2005 4:44 AM:
    >>but I cant run it on the global scope.

    >
    >
    > remove the "var" declorations before the variable names and they will
    > be global, just make sure they don't conflict with another variable
    > with the same name.
    >
    >
    >>Please quote what you are replying to.
    >>
    >>If you want to post a followup via groups.google.com, don't use the
    >>"Reply" link at the bottom of the article. Click on "show options" at
    >>the top of the article, then click on the "Reply" at the bottom of the
    >>article headers.

    >
    >
    > Lose the canned message text. If it were important to people other than
    > you it would be in the FAQ.


    With a response like that, it becomes obvious that you have not read the
    FAQ that you say it would be in because it *is* in the FAQ. Since you
    can not seem to find it or want to find it, let me give you the driect
    anchor link for the pertinent section:

    <URL: http://jibbering.com/faq/#FAQ2_3 >

    And let me quote part of that section for you, specifically paragraph 6:
    <quote>
    When replying to a message on the group trim quotes of the preceding
    messages to the minimum needed and add your comments below the pertinent
    section of quoted material, as per FYI28/RFC1855 (never top post).
    </quote>

    The snippet about how to post on Google Groups is a way of letting
    Google posters know how to quote without trying to be rude. I could of
    course just say "Read the f**king FAQ" but I prefer to be a little more
    helpful than that.

    As for that snippet, it is indeed up for consideration in the next FAQ
    Update. And if you wish, when it comes up, you are welcome to argue
    against the groups desires for people to quote. I doubt you will get far
    but it is your choice and opportunity to do so.

    Watch for a thread entitled FAQ Updates or something to that effect.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
     
    Randy Webb, Oct 27, 2005
    #8
  9. On Wed, 26 Oct 2005 21:08:47 -0700, Farhadi wrote:

    > before, I had included my js files thruogh Script block at the HEAD.
    >
    > now I want to include them using XMLHTTPRequest [..]


    For another alternative, you can read scriptaculous.js.

    Sincerely,
    Joachim
     
    Joachim Zobel, Oct 27, 2005
    #9
  10. On Wed, 26 Oct 2005 21:08:47 -0700, Farhadi wrote:

    > before, I had included my js files thruogh Script block at the HEAD.
    >
    > now I want to include them using XMLHTTPRequest because of 2 reason: 1. I
    > want to load scripts on onload event of body. to speed up loading of page.
    > 2. I dont want my scripts be saveable thruogh "Save As" at browser.


    Do you really think these reasons are worth the effort and the additional
    error prone complexity? 1. will not give you much unless you have >100kb
    of script code. 2. is just a luser stopper. Real men GET with telnet.

    > and I want to run my script just like including them on a script block.
    > is there any more ways?


    You could try including with script blocks at runtime by adding
    those script tags to the head using DOM.

    Sincerely,
    Joachim
     
    Joachim Zobel, Oct 27, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Newton
    Replies:
    3
    Views:
    9,591
    Brock Allen
    Apr 4, 2005
  2. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    577
    Karl Seguin [MVP]
    Jun 16, 2006
  3. Alex van der Spek

    eval('07') works, eval('08') fails, why?

    Alex van der Spek, Jan 8, 2009, in forum: Python
    Replies:
    6
    Views:
    1,543
    Bruno Desthuilliers
    Jan 8, 2009
  4. Liang Wang
    Replies:
    8
    Views:
    152
    Ben Morrow
    Feb 2, 2008
  5. Marc Girod

    to eval or not to eval?

    Marc Girod, Apr 19, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    175
    Marc Girod
    Apr 19, 2011
Loading...

Share This Page