Connecting Tech Pros Worldwide Forums | Help | Site Map

loading js code with xmlhttp working (but..)

warteschlange
Guest
 
Posts: n/a
#1: Jul 23 '05
i want to postload javscript from another javascript.
This works fine in
firefox and IE6
for macIE i can use an Iframe to load the code and inject it with
insertAdjacentHTML
The problems arise with safari and opera.
both load the new code with XMLHttpRequest, but the code is no
'executable'
To make this possible on IE i had to use the magic 'DEFER' attribute.

(Sync or Async ist not the issue)



This is a extract of the working code:
Expand|Select|Wrap|Line Numbers
  1. //----------------
  2. function importJS(url){
  3. var script = syncGetFile(url);
  4. document.body.insertAdjacentHTML("beforeEnd",
  5. "<span>&nbsp;</span><SCRIPT DEFER>" + script + "</SCRIPT>");
  6. }
  7.  
  8. //----------------
  9. function syncGetFile(url){
  10. var xmlhttp = _XMLHttpRequest_init();
  11. xmlhttp.open("GET", url, false);
  12. xmlhttp.send(null);
  13. return xmlhttp.responseText;
  14. }
  15. function _XMLHttpRequest_init() {
  16. .....
  17. }
  18. if(typeof HTMLElement!="undefined" && !
  19. HTMLElement.prototype.insertAdjacentElement){
  20. HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) {
  21. }
  22. }
  23.  
for testing try to insert "var str = 'function a(){alert(1)}'; with
insertAdjacent or another way
and call the function afterwards.

i'm stuck now.

opera and safari experts needed...
Andres


commercial
Guest
 
Posts: n/a
#2: Jul 23 '05

re: loading js code with xmlhttp working (but..)


If your problem is conditional script content because of

different agents, use comment syntax like

<!-- [if >IE5 ..] -->


-------------------

If you don't understand "remote scripting",
aka xmlHttp, study hard and find out.

--------------------

hundreds of thousands of people died in my lifetime
(outside US borders) for US one way street thinking.


See now, what your kind did to humans.

Chimpanses have 6 genes different from those of humans
of some 35.000 genes total. And your post just shows that.






"warteschlange" <andres@holzapfel.ch> wrote in message
news:1121589276.447914.19820@g49g2000cwa.googlegro ups.com...[color=blue]
> i want to postload javscript from another javascript.
> This works fine in
> firefox and IE6
> for macIE i can use an Iframe to load the code and inject it with
> insertAdjacentHTML
> The problems arise with safari and opera.
> both load the new code with XMLHttpRequest, but the code is no
> 'executable'
> To make this possible on IE i had to use the magic 'DEFER' attribute.
>
> (Sync or Async ist not the issue)
>
>
>
> This is a extract of the working code:
>
Expand|Select|Wrap|Line Numbers
  1. > //----------------
  2. > function importJS(url){
  3. > var script = syncGetFile(url);
  4. > document.body.insertAdjacentHTML("beforeEnd",
  5. > "<span>&nbsp;</span><SCRIPT DEFER>" + script + "</SCRIPT>");
  6. > }
  7. >
  8. > //----------------
  9. > function syncGetFile(url){
  10. > var xmlhttp = _XMLHttpRequest_init();
  11. > xmlhttp.open("GET", url, false);
  12. > xmlhttp.send(null);
  13. > return xmlhttp.responseText;
  14. > }
  15. > function _XMLHttpRequest_init() {
  16. > ....
  17. > }
  18. > if(typeof HTMLElement!="undefined" && !
  19. > HTMLElement.prototype.insertAdjacentElement){
  20. > HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) {
  21. >  }
  22. > }
> for testing try to insert "var str = 'function a(){alert(1)}'; with
> insertAdjacent or another way
> and call the function afterwards.
>
> i'm stuck now.
>
> opera and safari experts needed...
> Andres
>[/color]

Martin Honnen
Guest
 
Posts: n/a
#3: Jul 23 '05

re: loading js code with xmlhttp working (but..)




warteschlange wrote:
[color=blue]
> i want to postload javscript from another javascript.
> This works fine in
> firefox and IE6
> for macIE i can use an Iframe to load the code and inject it with
> insertAdjacentHTML
> The problems arise with safari and opera.
> both load the new code with XMLHttpRequest, but the code is no
> 'executable'[/color]

With recent Opera (8.x, perhaps 7.5x) it should be possible to simply
create a new script element object and insert it into the page without
using any XMLHttpRequest e.g.

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'test2005071701.js';
document.getElementsByTagName('head')[0].appendChild(script);

That also works with IE/Win and with Mozilla but you should note that
the script file might be loaded asynchronously so don't expect to be
able to call functions in the JavaScript file directly after the
appendChild call.




--

Martin Honnen
http://JavaScript.FAQTs.com/
warteschlange
Guest
 
Posts: n/a
#4: Jul 23 '05

re: loading js code with xmlhttp working (but..)


thanks,
i will try Opera 8.
I'm completely aware of the sync / async problem.

So there remains Safari as a problem.
I'm sure, that sooner or later Safari will allow your mentioned method,
but until then some hacking is needed.

(I guess, it is the same with the opacity - you have to convice the
progammers, that it is not relevant how useful that 'gadget' is, what
really matters is compability)

One aproach to make it work with safari, was loading the new jssrc and
using eval (arghhh...) - Works, but functions must be defined this
way:
window.funcName = function(...){...}, to be callable from the origin
code.

Again thanks
and hoping for a Safari expert

warteschlange
Guest
 
Posts: n/a
#5: Jul 23 '05

re: loading js code with xmlhttp working (but..)


I noticed one disadvatage of your method to use
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'test2005071701.js';
instead of httpRequest:
you see in the statusbar an information, that a reload is goin on...
Having a timer for requesting data every few seconds this is
unfortunately not very elegant!

Randy Webb
Guest
 
Posts: n/a
#6: Jul 23 '05

re: loading js code with xmlhttp working (but..)


warteschlange said the following on 7/22/2005 2:12 PM:
[color=blue]
> I noticed one disadvatage of your method to use
> var script = document.createElement('script');
> script.type = 'text/javascript';
> script.src = 'test2005071701.js';
> instead of httpRequest:
> you see in the statusbar an information, that a reload is goin on...
> Having a timer for requesting data every few seconds this is
> unfortunately not very elegant![/color]

It is still a better alternative than an HTTPRequest that has limited
support though, isn't it? createElement is supported in more browsers
than HTTPRequest is. Possibly using a test for HTTPRequest support and a
final section that attempts to use createElement and possibly two other
ways to dynamically load a .js file.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Closed Thread