[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

dynamic function

Luuk

1/11/2015 3:34:00 PM



I have jsHandler.js (found on the internet) with this:
function include(filename)
{
var head = document.getElementsByTagName('head')[0];

script = document.createElement('script');
script.src = filename;
script.type = 'text/javascript';

head.appendChild(script)
}

I have a file called testje.js with this:
function testje() {
document.write('testje');
}


My html looks like this:
<!DOCTYPE html>
<html>
<head>
<title>testje</title>

<script src="jsHandler.js"></script>
<script>
include('testje.js');
</script>
<body onload='testje();'>
</body>
</html>


IE complains about 'testje' is undefined.

What i want is to be able to execute the call the function 'testje',
which is defined inside the 'testje.js'

This will lead to an htmlpage which will be opened locally on a computer
(not via a web server)

Can anyone give a hint on how to get this done?


5 Answers

Martin Honnen

1/11/2015 4:42:00 PM

0

Luuk wrote:
>
>
> I have jsHandler.js (found on the internet) with this:
> function include(filename)
> {
> var head = document.getElementsByTagName('head')[0];
>
> script = document.createElement('script');
> script.src = filename;
> script.type = 'text/javascript';
>
> head.appendChild(script)
> }
>
> I have a file called testje.js with this:
> function testje() {
> document.write('testje');
> }
>
>
> My html looks like this:
> <!DOCTYPE html>
> <html>
> <head>
> <title>testje</title>
>
> <script src="jsHandler.js"></script>
> <script>
> include('testje.js');
> </script>
> <body onload='testje();'>
> </body>
> </html>
>
>
> IE complains about 'testje' is undefined.

Which version of IE?

> What i want is to be able to execute the call the function 'testje',
> which is defined inside the 'testje.js'

Why can't you use

<script src="testje.js"></script>

?

And why do you use "createElement" and "appendChild" in one function to
create elements to then try to call a function "onload" that does a
"document.write", which would overwrite the existing document? That all
does not make sense to me.

If you want to call a function loaded by a dynamically created and
inserted "script" element you might be better off to set up a "load"
event listener on that "script" element and call any function in the
event listener.






--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

JJ

1/11/2015 7:02:00 PM

0

On Sun, 11 Jan 2015 16:33:43 +0100, Luuk wrote:
> I have jsHandler.js (found on the internet) with this:
> function include(filename)
> {
> var head = document.getElementsByTagName('head')[0];
>
> script = document.createElement('script');
> script.src = filename;
> script.type = 'text/javascript';
>
> head.appendChild(script)
> }
>
> I have a file called testje.js with this:
> function testje() {
> document.write('testje');
> }
>
> My html looks like this:
> <!DOCTYPE html>
> <html>
> <head>
> <title>testje</title>
>
> <script src="jsHandler.js"></script>
> <script>
> include('testje.js');
> </script>
> <body onload='testje();'>
> </body>
> </html>
>
> IE complains about 'testje' is undefined.
>
> What i want is to be able to execute the call the function 'testje',
> which is defined inside the 'testje.js'
>
> This will lead to an htmlpage which will be opened locally on a computer
> (not via a web server)
>
> Can anyone give a hint on how to get this done?

Do both JS have actually been loaded by IE?

Luuk

1/11/2015 8:20:00 PM

0

On 11-1-2015 17:41, Martin Honnen wrote:
> Luuk wrote:
>>
>>
>> I have jsHandler.js (found on the internet) with this:
>> function include(filename)
>> {
>> var head = document.getElementsByTagName('head')[0];
>>
>> script = document.createElement('script');
>> script.src = filename;
>> script.type = 'text/javascript';
>>
>> head.appendChild(script)
>> }
>>
>> I have a file called testje.js with this:
>> function testje() {
>> document.write('testje');
>> }
>>
>>
>> My html looks like this:
>> <!DOCTYPE html>
>> <html>
>> <head>
>> <title>testje</title>
>>
>> <script src="jsHandler.js"></script>
>> <script>
>> include('testje.js');
>> </script>
>> <body onload='testje();'>
>> </body>
>> </html>
>>
>>
>> IE complains about 'testje' is undefined.
>
> Which version of IE?

IE 11 (Windows 7)

>
>> What i want is to be able to execute the call the function 'testje',
>> which is defined inside the 'testje.js'
>
> Why can't you use
>
> <script src="testje.js"></script>
>
> ?

Because in the end i first want to determine the name of the 'extra'
javascript to be loaded, and after that i want to load the appropriate
script.

>
> And why do you use "createElement" and "appendChild" in one function to
> create elements to then try to call a function "onload" that does a
> "document.write", which would overwrite the existing document? That all
> does not make sense to me.
>

sorry, my example was bad .... ;-)

> If you want to call a function loaded by a dynamically created and
> inserted "script" element you might be better off to set up a "load"
> event listener on that "script" element and call any function in the
> event listener.

my javascript knowledge is letting /me down on this one.
but i hope i know what you mean by that tomorrow ;-)

>
>
>
>
>
>
> --- news://freenews.netfront.net/ - complaints: news@netfront.net ---

Martin Honnen

1/12/2015 9:42:00 AM

0

Luuk wrote:
> On 11-1-2015 17:41, Martin Honnen wrote:
>> Luuk wrote:

>>> IE complains about 'testje' is undefined.
>>
>> Which version of IE?
>
> IE 11 (Windows 7)

What happens when you implement your include function in the external
file as

function include(scriptSrc) {
document.write('<script src="' + scriptSrc + '"></script>');
}

? Do you still get the error?


--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

Luuk

1/12/2015 7:44:00 PM

0

On 12-1-2015 10:41, Martin Honnen wrote:
> Luuk wrote:
>> On 11-1-2015 17:41, Martin Honnen wrote:
>>> Luuk wrote:
>
>>>> IE complains about 'testje' is undefined.
>>>
>>> Which version of IE?
>>
>> IE 11 (Windows 7)
>
> What happens when you implement your include function in the external
> file as
>
> function include(scriptSrc) {
> document.write('<script src="' + scriptSrc + '"></script>');
> }
>
> ? Do you still get the error?
>

that 'does not work' too.

Sorry for this answer.

But i see i have to do another approach to my problem.

I have a static piece of html,
with some javascript (http://visjs.org/timeline_exa...)

My data is (or will be) dynamic.

I'm trying to load a page with this dynamic data, *without* using a web
server. (different users should have different data)