473,511 Members | 12,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Defered loading of external Javascript files and Safari

Hi,

I want to load an external javascript file, get its results and stick
them inside a <div> block. I also
want to do this in several places on a web page.

This way the browser doesn't have to wait for the external resource to
load to show up the page,
thus giving a perceiving faster load time for the user.

Since i didn't found any way to do this (even the MSIE DEFER attribute
would mess everything up if
the javascript ever used a document.write() - and we use those a lot),
i came up with the following
technique:

1 - Create a placeholder where you want code generated by the external
js file. You can also
place an image or content announcing to the user that the page is
"Loading.. ".
Use the title attribute uniquely. Use the same unique name for
'name' and 'id' - MSIE needs
this to get document.getElementsByName to work properly:

<div title="180x150" id="advertisement" name="advertisement"
style="width:180px;height:150px;padding:1px;border :1px solid
#CCC;float:left;">
<img src="http://imgs.sapo.pt/images/2004/hp/pub180x150.gif"
width="180" height="150" border="0" />
</div>

2 - At the bottom of the document create a hidden div block which
points to the javascript resource you wish to load. The attribute 'id'
must map to the 'title' attribute defined above :

<div id="180x150" style="display:none">
<script src="http://foo.com/myjs.js"></script>
</div>

3 - Paste the following code right before </body> to load the
javascript files in the right place.
This uses the innerHTML method to replace code from the hidden
div to the placeholder on the main document.:

<script language="JavaScript1.1" type="text/javascript">
//<![CDATA[
var ar = document.getElementsByName('publicidade');
for( i=0; i<ar.length; i++) ar[i].innerHTML =
document.getElementById(ar[i].title).innerHTML;
//]]>
</script>
We are using this technique in http://www.sapo.pt/index.html (don't try
it on Safari, you won't get your commercial ads. ) . Hit reload and see
how
the ads appear after the page is fully displayed. They were loaded via
an adserver that
sends javascript with html encoded inside a document.write().

This technique works well in Firefox 1 and MSIE6, but not in Safari. I
think that Safari doesn't parse well the DOM tree, thus leaving empty
the hidden div's.

I'd love to hear some comments and feedback from the community on the
technique and how to get it to work on Safari.

Thanks in Advance,

Joao Pedro Goncalves
Portugal Telecom
http://www.sapo.pt/

Jul 23 '05 #1
5 5182
I might be missing the point here, but wouldn't it be easier to just
use iframes to load your remote content?

Jul 23 '05 #2
Well, you have the right idea about wanting to reduce
load times. One of THE rudest things on the internet
is where you have to sit and wait to use a site because
the adserver can't be bothered to load its ad (quickly
or at all). It's one thing to show an ad. It's quite
another where ad tardiness starts to cripple your own
site.

Your ad is not displaying at all on my Firefox 1.0.1+ (a
nightly build from March 20), though its placeholder
is there (ie. space for a blank pic sans frame).

Some possible background reading for ideas:
http://groups-beta.google.com/group/...4f6806b71219c/

However, isn't what you are doing overblown (and also
crippling to several browsers)? Why not just do a
<body onLoad="now load the images">, or if you need
javascript (isn't an ad having javascript going too far?)
why not stuff things into an iframe and set the iframe's
source using the same type of <body onLoad=...> construct.

Csaba Gabor from Vienna
jo****************@gmail.com wrote:
Hi,

I want to load an external javascript file, get its results and stick
them inside a <div> block. I also
want to do this in several places on a web page.

This way the browser doesn't have to wait for the external resource to
load to show up the page,
thus giving a perceiving faster load time for the user.

Since i didn't found any way to do this (even the MSIE DEFER attribute
would mess everything up if
the javascript ever used a document.write() - and we use those a lot),
i came up with the following
technique:

1 - Create a placeholder where you want code generated by the external
js file. You can also
place an image or content announcing to the user that the page is
"Loading.. ".
Use the title attribute uniquely. Use the same unique name for
'name' and 'id' - MSIE needs
this to get document.getElementsByName to work properly:

<div title="180x150" id="advertisement" name="advertisement"
style="width:180px;height:150px;padding:1px;border :1px solid
#CCC;float:left;">
<img src="http://imgs.sapo.pt/images/2004/hp/pub180x150.gif"
width="180" height="150" border="0" />
</div>

2 - At the bottom of the document create a hidden div block which
points to the javascript resource you wish to load. The attribute 'id'
must map to the 'title' attribute defined above :

<div id="180x150" style="display:none">
<script src="http://foo.com/myjs.js"></script>
</div>

3 - Paste the following code right before </body> to load the
javascript files in the right place.
This uses the innerHTML method to replace code from the hidden
div to the placeholder on the main document.:

<script language="JavaScript1.1" type="text/javascript">
//<![CDATA[
var ar = document.getElementsByName('publicidade');
for( i=0; i<ar.length; i++) ar[i].innerHTML =
document.getElementById(ar[i].title).innerHTML;
//]]>
</script>
We are using this technique in http://www.sapo.pt/index.html (don't try
it on Safari, you won't get your commercial ads. ) . Hit reload and see
how
the ads appear after the page is fully displayed. They were loaded via
an adserver that
sends javascript with html encoded inside a document.write().

This technique works well in Firefox 1 and MSIE6, but not in Safari. I
think that Safari doesn't parse well the DOM tree, thus leaving empty
the hidden div's.

I'd love to hear some comments and feedback from the community on the
technique and how to get it to work on Safari.

Thanks in Advance,

Joao Pedro Goncalves
Portugal Telecom
http://www.sapo.pt/

Jul 23 '05 #3
We used to have iframes, but some ad campaigns are javascript and flash
based. It just didn't work the way it was supposed to.

Iframes don't resize properly for text ads too. Google's adsense also
work using external javascript calls, for instance.

Jul 23 '05 #4
> Some possible background reading for ideas:
http://groups-beta.google.com/ group/comp.lang.javascript/bro wse_frm/t...
Thank you very much for pointing me to the discussion, but like Michael
Winter points out,
" you should be aware that the SCRIPT
element doesn't support an id attribute under any version of HTML or
XHTML".

I've also tried using XMLHTTPRequest and eval() (or changing the
mimetype to text/javascript), but
Firefox only allows connections from the same host:port as the original
page. We have more than 200 servers and our sysadmin guys would
certainly hate having to install reverse proxies for the adservers on
every place we use this for deferring javascript loading.
However, isn't what you are doing overblown (and also
crippling to several browsers)? Why not just do a
<body onLoad="now load the images">, or if you need
javascript (isn't an ad having javascript going too far?)
why not stuff things into an iframe and set the iframe's
source using the same type of <body onLoad=...> construct.


It's a web portal with lots of flash ads, even videos,
we've used iframes until two/three years ago, it doesn't resize
properly
and it doesn't work for text ads. On the http://www.sapo.pt/ page we
even have
a <span> tag loading a text ad.

We have more than 1 million pageviews a day and the only complains we
had so far were
from Safari users - such as myself.

Joao Pedro Goncalves

Jul 23 '05 #5
jo****************@gmail.com wrote:
Hi, [...] 1 - Create a placeholder where you want code generated by the external
js file. You can also
place an image or content announcing to the user that the page is
"Loading.. ".
Use the title attribute uniquely. Use the same unique name for
'name' and 'id' - MSIE needs
this to get document.getElementsByName to work properly:

<div title="180x150" id="advertisement" name="advertisement"
The 'name' attribute is not allowed on div elements.

<URL:http://www.w3.org/TR/html4/index/attributes.html>

Getting getElementsByName to work at all on elements that aren't
supposed to have a name is depending on non-specified behaviour.

[...] 2 - At the bottom of the document create a hidden div block which
points to the javascript resource you wish to load. The attribute 'id'
must map to the 'title' attribute defined above :

<div id="180x150" style="display:none">
ids and names must start with a letter, not a number.

<URL:http://www.w3.org/TR/html4/types.html#type-name>
Since you are giving the browser invalid HTML, then what the browser
does with it can't be relied upon to be consistent between browsers.

[...]
We are using this technique in http://www.sapo.pt/index.html (don't try
it on Safari, you won't get your commercial ads. ) . Hit reload and see


Safari users may not see that as a 'problem' ;-)

[...]

--
Rob
Jul 23 '05 #6

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

Similar topics

17
2747
by: CES | last post by:
All, I was wondering if their is a way of loading an external script fill from within a script?? <script language="javascript" type="text/javascript"> function test(var){ <script...
1
3779
by: ozzy.osborn | last post by:
Hello All, I have been struggling with a cross browser solution to loading external javascript files on the fly. I have been successful using the following code in IE6: var newScr =...
2
1869
by: www.gerardvignes.com | last post by:
I am using this to load the client JavaScript for a web application when it is selected by the user) via an Ajax connection to the server. I have found only two ways of loading new JavaScript...
6
2742
by: Venkatesh | last post by:
Hello All, I have couple of doubts regarding the concept of on-demand javascript loading using javascript code. I could see on the net different techniques for achieving this - techniques like:...
1
3397
by: agatha.life | last post by:
I did a javascript for the loading of images (I didn't want to have the images loaded in "on loading" because they are too many). The website is for a model and if you look at the codeof pages (...
6
2197
by: ernesto.tejeda | last post by:
Hello, I have a couple of questions regarding the loading of .js files for the browser and would like anyone to point me wher to find the answer (or if you know the answer and tell me will do just...
1
1461
by: Danny | last post by:
Hey everybody, I am a complete newbie with Javascript but was wondering if anybody could tell me how to load pages dynamicly within a div? Here is the site: http://regiesrule.info, i would like...
20
4001
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...
1
7404
by: npm | last post by:
Hi I've checked out this page (and it works fine) in FF, IE, Opera, Chrome and Safari: http://www.w3schools.com/dom/tryit.asp?filename=try_dom_list_loop But when I try and tweak it for a site...
0
7144
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
7427
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...
1
7085
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
7512
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
5671
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,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.