473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

loading js code with xmlhttp working (but..)

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

Jul 23 '05 #1
5 3319
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" <an****@holzapfel.ch> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
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.  function syncGetFile(url){
  9.  var xmlhttp = _XMLHttpRequest_init();
  10.  xmlhttp.open("GET", url, false);
  11.  xmlhttp.send(null);
  12.  return xmlhttp.responseText;
  13.  }
  14.  function _XMLHttpRequest_init() {
  15.  ....
  16.  }
  17.  if(typeof HTMLElement!="undefined" && !
  18.  HTMLElement.prototype.insertAdjacentElement){
  19.  HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) {
  20.   }
  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


Jul 23 '05 #2


warteschlange wrote:
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'


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/
Jul 23 '05 #3
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

Jul 23 '05 #4
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!

Jul 23 '05 #5
warteschlange said the following on 7/22/2005 2:12 PM:
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!


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
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: sneill | last post by:
Using XMLHTTP and DOM I'm able to load new HTML page content. I'd now like to load small snippets of javascript with the HTML markup and have that <script> incorporated into the page. If any of...
4
by: blackhawk | last post by:
I need to build a web page that has to potentially display a large amount of data in two grids on the same page. The HTML file with all of the formatting is about 7MB in size. This is too large...
9
by: WanamakerMedia | last post by:
I've posted this in at least 6 other forums with no responses. I am completely new to AJAX, so I got this code from another site and installed it for what I need. It works great if I have only one...
6
by: Shigun | last post by:
On a website I am working on I am trying to load another page into a div on the the page the user does his work from. What I have works correctly in FireFox, but not in IE. I've rummaged Google for...
20
RMWChaos
by: RMWChaos | last post by:
Currently testing in: WinVista / IE7 I have been working on getting xmlhttprequest going for weeks now. I have finally gotten a semi-working script going. Pulling up text or xml files works great...
2
by: joe | last post by:
I am loading a text file to a variable with XMLHttpRequest() There seems to be some sort of timing issue since loadXML (source code below) returns the contents of the file on seconds try. In...
5
by: TMN | last post by:
Hi All If I want to use an xml file to hold some config parameters for a web page do I need to load it with JS or is there a way of including it in the <head? Thanks Tim
5
by: mukeshrasm | last post by:
Hi I am using AJAX to display the value in selection/list box. this code is working fine in Firefox Mozila browser but it is not working in Internet Explorer so please tell me how this will work...
1
by: kout | last post by:
Hello. I'm trying to make some kind of image gallery with php and javascript (w/ ajax), but I've run into some problems. The basic idea is the following: I've got a <span> tag and, when I click a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.