From: Question Boy on
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
From: Sean Kinsey on
On Apr 8, 7:55 pm, Question Boy <question....(a)hotmail.com> wrote:
> 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('');
From: Sean Kinsey on
On Apr 8, 7:55 pm, Question Boy <question....(a)hotmail.com> wrote:
> 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?

From: Jeremy J Starcher on
On Thu, 08 Apr 2010 10:55:50 -0700, Question Boy wrote:

> 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.
From: Luuk on
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...



--
Luuk