473,788 Members | 3,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2401
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.javas cript 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.c om, 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.javas cript 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.c om/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(n ull); 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.c om, 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.c om, 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.javas cript 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.j s.

Sincerely,
Joachim
Oct 27 '05 #10

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

Similar topics

4
2032
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
3458
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 properly. It works, for example, when invoking functions (alert('hello')), but not for defining functions. The case occurs when retrieving the javascript code with
4
6584
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 myVar1 = 1; var myVar2 = 3; window.someFunc = function() { myVar1++; alertFunc(); }; function anotherFunc() { myVar2++; alertFunc(); }
3
1919
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 arrays each select is individually named withe MySelecti where i is an incremental from 1. I know all my variables are correct (i, OptionsCount) and my arrays of MyValues and MyDescription's exist for multiple enteries and if I bring out an...
7
3965
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 method access" for the second to last line of my function. If you know what I've done wrong or how to fix it I would appreciate the help.
2
3684
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
29382
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
10
494
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 the different types of objects you can create as values. I really don't like using eval, and it's causing problems, like if I do something like the following:
4
1988
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
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.