Execute code from text file

Q

Question Boy

I have a particular need to pull some javascript from a text file and
run it within an html page.

<script>
function somefuntion(){
...
}
</script>

Let say I have a text file (js01.txt) with proper javascript within
it, say for illustrative purposes:

alert('This is a simple alert message!');

Is there a way to pull this into the somefuntion() above and execute
it. Keeping the html doc untouched but allowing me to update the txt
as needed.

Thank you for your guidance. It is truly appreciated.

QB
 
S

Sean Kinsey

I have a particular need to pull some javascript from a text file and
run it within an html page.

<script>
function somefuntion(){
    ...}

</script>

Let say I have a text file (js01.txt) with proper javascript within
it, say for illustrative purposes:

alert('This is a simple alert message!');

Is there a way to pull this into the somefuntion() above and execute
it.  Keeping the html doc untouched but allowing me to update the txt
as needed.

Thank you for your guidance.  It is truly appreciated.

QB

This is one way to do it
var xhrObj = createXMLHTTPObject();
xhrObj.onreadystatechange = function(){
if (xhrObj.readyState == 4 && xhrObj.status >=200 && xhrObj.status
< 300) {
xhrObj.onreadystgatechange = null;
eval(xhrObj.responseText);
};
xhrObj.open('GET', "textfile.txt", true);
xhrObj.send('');
 
S

Sean Kinsey

Is there a way to pull this into the somefuntion() above and execute
it.  Keeping the html doc untouched but allowing me to update the txt
as needed.

But why do it this way?
Why not use a regular js file, and include this?
 
J

Jeremy J Starcher

I have a particular need to pull some javascript from a text file and
run it within an html page.

<script>
function somefuntion(){
...
}
</script>

Let say I have a text file (js01.txt) with proper javascript within it,
say for illustrative purposes:

alert('This is a simple alert message!');

Is there a way to pull this into the somefuntion() above and execute it.
Keeping the html doc untouched but allowing me to update the txt as
needed.

Thank you for your guidance. It is truly appreciated.

QB

While dynamic script loading is possible, it has a number of landmines
that can catch you unaware. You'd be MUCH better off to inject the
script element into your generated HTML server-side.

Wrap it in a function and then call the function when needed.

If that solution doesn't work, I'd need more information before I could
assist you.
 
L

Luuk

Op 8-4-2010 19:55, Question Boy schreef:
I have a particular need to pull some javascript from a text file and
run it within an html page.

<script>
function somefuntion(){
...
}
</script>

Let say I have a text file (js01.txt) with proper javascript within
it, say for illustrative purposes:

alert('This is a simple alert message!');

Is there a way to pull this into the somefuntion() above and execute
it. Keeping the html doc untouched but allowing me to update the txt
as needed.

Thank you for your guidance. It is truly appreciated.

QB


<html>
<head>

<script>

var x = 'alert(\'testmessage\');';

function testit() {

var f = new Function("", x );
f();
}

</script>
</head>
<body>
<span onclick="testit();">Click on me...</span>
</body>
</html>


But, like others said,
<script type="text/javascript" src="external.js"></script>

Is a much better way, and after all, this external.js is just another
textfile, which happend to contain some javascript...
 
Q

Question Boy

But why do it this way?
Why not use a regular js file, and include this?

The reason is that I wish to build a standard function and load the
varying part from a txt file which get generated by a 3rd party
software app.

Basically I can setup all of the basic command, but I need to get the
field value from another app which give them in a txt file.
 
D

Dr J R Stockton

In comp.lang.javascript message <41d2109b-8157-426a-86a6-036fb1fc44a7@v1
6g2000vba.googlegroups.com>, Thu, 8 Apr 2010 10:55:50, Question Boy
I have a particular need to pull some javascript from a text file and
run it within an html page.

As in <URL:http://www.merlyn.demon.co.uk/js-grphx.htm> ? That does not
work in MSIE, but for reasons which do not affect file reading and
execution.
 
T

Thomas 'PointedEars' Lahn

Question Boy wrote:
^^^^^^^^^^^^
It is unlikely that you will be taken seriously here with that name.
The reason is that I wish to build a standard function and load the
varying part from a txt file which get generated by a 3rd party
software app.

Basically I can setup all of the basic command, but I need to get the
field value from another app which give them in a txt file.

You need to be more specific. "Field value"? What exactly does the
"txt file" look like? Can the "other app" generate script code?

<http://www.catb.org/~esr/faqs/smart-questions.html>


PointedEars
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top