P
Paul Gorodyansky
Hi,
User call my Virtual Keyboard by pressing a button. But many user may never need it,
so I don't want to load 100k of JavaScript code _when page is being loaded_ -
I want to load my JavaScript code only when a person clicks that button -
is it possible?
<head>
....
/* very small .js with variables initiation and v_load() function */
<script type="text/javascript" src="Vk_init.js"></script>
....
</head>
<body>
....
<button id="vk" onclick="v_load(); v_showKbd">Virtual Keyboard</button>
....
v_load() loads all 100k of JavaScript - v_code.js file -
including the function v_showKbd()
But whatever I do, button click produces JavaScript error -
"v_showKbd is undefined" - as well as any variable initiated in v_code.js
if say I place alert() with it into the same button's code
So how to make v_load()-produced stuff be known to the JavaScript in the same
button?
If I load v_code.js in <head> then, surely, v_showKbd() on a button click
works just fine, but I don't want to load 100k v_code.js such way -
it may never be needed...
I tried these two approaches for v_load() which loads v_code.js but both of them failed,
that is, still get JavaScript error "v_showKbd undefined" when click the button:
1) v_load() runs and adds a child to <BODY> which loads .js file:
v_bobj = document.getElementsByTagName('body')[0];
script1 = document.createElement('script');
script1.type = 'text/javascript';
script1.src = "v_code.js";
v_bobj.appendChild(script1);
That is, a child like
<script type="text/javascript" src="v_code.js"></script>
Nope. I tried the same with adding such child to <head> object - same negative result -
that is, v_code.js _is_ loaded - I placed alert("***"); there at the top, BUT its
functions and variables are unknown in the Button's onclick() code -
I press the button and get "v_showKbd() is undefined
2) Tried to add in v_load() that not as DOM object but as HTML using existing (empty)
<div id="v_divTmp">:
v_tmp = document.getElementById("v_divTmp");
v_add = "<scri"+"pt type='text/javascript' src='v_code.js'></scr"+"ipt>";
v_tmp.innerHTML=v_add;
Same negative result - "v_showKbd is undefined
(side note: and this time looks like v_code.js was not loaded at all -
its alert("**"); did not work).
Any suggestions? Should be kind of common issue, I guess...
User call my Virtual Keyboard by pressing a button. But many user may never need it,
so I don't want to load 100k of JavaScript code _when page is being loaded_ -
I want to load my JavaScript code only when a person clicks that button -
is it possible?
<head>
....
/* very small .js with variables initiation and v_load() function */
<script type="text/javascript" src="Vk_init.js"></script>
....
</head>
<body>
....
<button id="vk" onclick="v_load(); v_showKbd">Virtual Keyboard</button>
....
v_load() loads all 100k of JavaScript - v_code.js file -
including the function v_showKbd()
But whatever I do, button click produces JavaScript error -
"v_showKbd is undefined" - as well as any variable initiated in v_code.js
if say I place alert() with it into the same button's code
So how to make v_load()-produced stuff be known to the JavaScript in the same
button?
If I load v_code.js in <head> then, surely, v_showKbd() on a button click
works just fine, but I don't want to load 100k v_code.js such way -
it may never be needed...
I tried these two approaches for v_load() which loads v_code.js but both of them failed,
that is, still get JavaScript error "v_showKbd undefined" when click the button:
1) v_load() runs and adds a child to <BODY> which loads .js file:
v_bobj = document.getElementsByTagName('body')[0];
script1 = document.createElement('script');
script1.type = 'text/javascript';
script1.src = "v_code.js";
v_bobj.appendChild(script1);
That is, a child like
<script type="text/javascript" src="v_code.js"></script>
Nope. I tried the same with adding such child to <head> object - same negative result -
that is, v_code.js _is_ loaded - I placed alert("***"); there at the top, BUT its
functions and variables are unknown in the Button's onclick() code -
I press the button and get "v_showKbd() is undefined
2) Tried to add in v_load() that not as DOM object but as HTML using existing (empty)
<div id="v_divTmp">:
v_tmp = document.getElementById("v_divTmp");
v_add = "<scri"+"pt type='text/javascript' src='v_code.js'></scr"+"ipt>";
v_tmp.innerHTML=v_add;
Same negative result - "v_showKbd is undefined
(side note: and this time looks like v_code.js was not loaded at all -
its alert("**"); did not work).
Any suggestions? Should be kind of common issue, I guess...