how to eval in the global scope

A

a.farhadi

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

Randy Webb

(e-mail address removed) 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.
 
F

Farhadi

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?
 
F

Farhadi

Randy said:
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?
 
R

RobG

Farhadi said:
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>
 
J

Julian Turner

RobG said:
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
 
J

Joshie Surber

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

Randy Webb

Joshie Surber said the following on 10/27/2005 4:44 AM:
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.




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

Joachim Zobel

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
 
J

Joachim Zobel

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top