473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to eval in the global scope

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 '05 #1
10 2351
a.*******@gmail.com 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
Oct 27 '05 #2
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?

Oct 27 '05 #3
Farhadi said the following on 10/26/2005 11:31 PM:

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.
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 27 '05 #4

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?

Oct 27 '05 #5
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

Oct 27 '05 #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

Oct 27 '05 #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.

Oct 27 '05 #8
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
Oct 27 '05 #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
Oct 27 '05 #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

Oct 27 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jean-Sébastien Bolduc | last post by:
Hello, I would like to associate a local namespace with a lambda function. To be more specific, here is exactly what I would want: def foo(): a = 1 f = lambda x : a*x return f
12
by: knocte | last post by:
Hello. I have always thought that the eval() function was very flexible and useful. If I use it, I can define functions at runtime!! However, I have found a case where eval() does not work...
4
by: jdc_1040 | last post by:
Could someone explain what the scope chain looks like to code running inside eval? (Please no "eval is the devil" essays :) ) Let's say I have a string of JavaScript that looks like this: var...
3
by: Pauljh | last post by:
Hi All, I'm running some javascript over a server side generated web page and have multiple generated empty select statements, that I want to populate when the page is loaded. As HTML doesn't do...
7
by: petermichaux | last post by:
Hi, I have tried the following based on suggestions of the best way to insert JavaScript into a page. This is instead of using eval(). Unfortunately IE says "unexpected call to property or...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
10
by: Gordon | last post by:
I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); objType is a select with...
4
by: Adam C. | last post by:
Mozilla.org suggests using the with statement to control bindings: var f = 2; with({f: 3}){ eval("f"); // evaluates to 3 } But that doesn't work for binding "this".
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.