473,508 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

load javascript asynchronously(without blocking browser)

Hello,
Maybe somebody already solved this task. I need to load JS
asynchronously without blocking browser. This means that I do not need
to block browser to load/render during loading script. The simple
construction to do this:

jsJSNode = document.createElement(“script”);
jsJSNode.type = “text/javascript”;
jsJSNode.src = “http:// den-01/Default.aspx“;
document.body.appendChild(jsMyNode);

works fine in IE6 & IE7. (By fine I mean – IE loads script
asynchronously and DOENT block the browser)

But the same code works synchronously in Firefox (blocks FF). The
strange situation because many websites say that download with be
asynchronously (e.g. http://ntt.cc/2008/02/10/4-ways-to-d...h-source.html).

My Default.aspx is:
protected void Page_Load(object sender, EventArgs e)
{
Thread.Sleep(200000); //just for show that FF is blocked
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/x-javascript";
Response.Write(“var var1 = 1;”);
}

And with this Default.aspx FF hangs. Does anybody knows how to solve
this task? (I can’t use XHR because there will be _crossdomain_ call
and this call will be blocked with FF anyway). I know that there is
attribute “defer” but it works for IE only.
Want to clarify task again: I need to get small JS that will contain
variable to my page. But this data is not critical and if server that
provides this data will go down for some reason I want to show page
anyway (the page and this data JS are in different domains). With IE
everything is fine but doesn’t work with FF (other browsers are not
critical at this moment)

Thank you,
Denis
Jun 27 '08 #1
9 3636
Denis wrote:
Maybe somebody already solved this task. I need to load JS
asynchronously without blocking browser. This means that I do not need
to block browser to load/render during loading script. The simple
construction to do this:

jsJSNode = document.createElement(“script”);
jsJSNode.type = “text/javascript”;
jsJSNode.src = “http:// den-01/Default.aspx“;
document.body.appendChild(jsMyNode);

works fine in IE6 & IE7. (By fine I mean – IE loads script
asynchronously and DOENT block the browser)

But the same code works synchronously in Firefox (blocks FF).
It wasn't even a good idea to begin with. Why not generate the `script'
element in the first place?
[...]
And with this Default.aspx FF hangs. Does anybody knows how to solve
this task?
XHR.
(I can’t use XHR
Yes, you can. But it makes everything only less compatible.
because there will be _crossdomain_ call
and this call will be blocked with FF anyway).
And IE and ...

However, you can access the server-side proxy script available with the same
protocol, domain and port component in the URI.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Jun 27 '08 #2
VK
On Apr 17, 3:43 pm, Denis <Samoi...@gmail.comwrote:
Hello,
Maybe somebody already solved this task. I need to load JS
asynchronously without blocking browser. This means that I do not need
to block browser to load/render during loading script. The simple
construction to do this:

jsJSNode = document.createElement(“script”);
jsJSNode.type = “text/javascript”;
jsJSNode.src = “http:// den-01/Default.aspx“;
document.body.appendChild(jsMyNode);
As you properly noticed, alas FF doesn't support "defer" attribute.
Try to add new nodes to the head section and not to the body.

var docHead = document.getElementsByTagName('head')[0];
docHead.normalize(); // before the first use
// it is not required but siggested
docHead.appendChild(jsMyNode);

be aware that there is some undocumented limit for <scriptand
<styleelements in the head section. I didn't find yet any official
numbers, but while developing JSONet
http://jsnet.sourceforge.net
IE6 hanged up sporadicly on 32<styleblocks and FF2.0 on about 52
blocks. This way if you are planning intensive script insertion, I
would highly suggest to provide finalize mechanics for used elements
(remove from the tree and kill all references). This is what I used
and ever after the program became stable on high load.
Jun 27 '08 #3
VK wrote:
On Apr 17, 3:43 pm, Denis <Samoi...@gmail.comwrote:
>Maybe somebody already solved this task. I need to load JS
asynchronously without blocking browser. This means that I do not need
to block browser to load/render during loading script. The simple
construction to do this:

jsJSNode = document.createElement(“script”);
jsJSNode.type = “text/javascript”;
jsJSNode.src = “http:// den-01/Default.aspx“;
document.body.appendChild(jsMyNode);

As you properly noticed, alas FF doesn't support "defer" attribute.
Try to add new nodes to the head section and not to the body.
This is obviously not going to make it any better, because then nothing is
displayed at all until the script has finished loading.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jun 27 '08 #4
It wasn't even a good idea to begin with. Why not generate the `script'
element in the first place?
What do you mean "generate"?
XHR.
Yes, you can. But it makes everything only less compatible.
No I can't - FF: "Permission denied to call method
XMLHttpRequest.open"
However, you can access the server-side proxy script available with the same
protocol, domain and port component in the URI.
Unfortunately not. I do not have access to server where page is
hosted. I can only ask webdevloper to put my JS on it. I have only
access to my server where only generated JS located.
Jun 27 '08 #5
Try to add new nodes to the head section and not to the body.
>
var docHead = document.getElementsByTagName('head')[0];
docHead.normalize(); // before the first use
// it is not required but siggested
docHead.appendChild(jsMyNode);
Thank you! I tried but this doesn't help - FF waits for script :( and
do not render anything.
be aware that there is some undocumented limit for <scriptand
<styleelements in the head section. I didn't find yet any official
numbers, but while developing JSONethttp://jsnet.sourceforge.net
IE6 hanged up sporadicly on 32<styleblocks and FF2.0 on about 52
blocks. This way if you are planning intensive script insertion, I
would highly suggest to provide finalize mechanics for used elements
(remove from the tree and kill all references). This is what I used
and ever after the program became stable on high load.
Good point, Thank you very much!
Jun 27 '08 #6
VK
On Apr 17, 4:27 pm, Denis <Samoi...@gmail.comwrote:
Try to add new nodes to the head section and not to the body.
var docHead = document.getElementsByTagName('head')[0];
docHead.normalize(); // before the first use
// it is not required but siggested
docHead.appendChild(jsMyNode);

Thank you! I tried but this doesn't help - FF waits for script :( and
do not render anything.
Wait a second. What do you actually mean by "blocking browser"? I
meant script insertion into DOM tree some later, after document load
event. In this case there should be no "blocking" unless issuing
synchronous XHR call.

For the initial document load the page is not rendered until browser
processes all instructions in window.onload handler. This way
window.onload = someFunction doesn't mean "show the page and do
someFunction". It means "do not display the page, do someFunction,
then display the page. So window.onload - if set - is treated as the
last chance to alter the document before actually show it. This
behavior consistent across all browsers - not FF only.

If one needs to initiate some scripting on document load yet he wants
to show the document right away, use overlay call to release the
graphics context:

window.onload = releaseContextAndInit;

function releaseContextAndInit() {
window.setTimeout('init()',10);
}

function init() {
// now take you time
// to do what you need
}
Jun 27 '08 #7
Denis wrote:
>It wasn't even a good idea to begin with. Why not generate the
`script' element in the first place?
What do you mean "generate"?
I took it you have a server-side script could generate the `script' element
server-side instead. My bad.
>XHR. Yes, you can. But it makes everything only less compatible.
No I can't - FF: "Permission denied to call method XMLHttpRequest.open"
This statement was made with the provision below, of course.
>However, you can access the server-side proxy script available with the
same protocol, domain and port component in the URI.
Unfortunately not. I do not have access to server where page is hosted. I
can only ask webdevloper to put my JS on it. I have only access to my
server where only generated JS located.
Ahh, now I see, it is the other way around? Well, then the problem would be
both on the part of the person who includes your script this way and your
ASP.NET resource. You should therefore ask in an ASP.NET group as the
problem would probably not be language-specific.

Using a registered MIME media type instead (recommended: text/javascript)
could help as well.

And please provide an attribution line next time.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jun 27 '08 #8
Wait a second. What do you actually mean by "blocking browser"?

Sorry, my fault. Have to be more descriptive.

I have number of pages that reside on many different web
servers(assume very big for patching server side for each). And I have
another one server (name it MyServer) that provides some information
(e.g. var RedirectTo = "www.google.com";). So all this pages (not
pages of course but their owners) agreed to include my small script at
the begging (right after <bodytag, I can change architecture at this
moment, this is requirement) and do redirection if needed. Also there
is another requirement: if MyServer is expecting any troubles with
response these pages have to load as usual (they all grant me 10ms to
make or not make redirection).
With IE I simple don't have issues - it loads JS from MyServer in
separate thread and I just check (several times during 10ms) is
variable here or not. If not here in 10 ms I allow page to be rendered
and than don't think about this variable. So with such task it is not
good to wait till whole page loaded and than do redirection (bad
customer experience).

-Denis
Jun 27 '08 #9
Denis wrote:
>Wait a second. What do you actually mean by "blocking browser"?

Sorry, my fault. Have to be more descriptive.

I have number of pages that reside on many different web
servers(assume very big for patching server side for each). And I have
another one server (name it MyServer) that provides some information
(e.g. var RedirectTo = "www.google.com";). So all this pages (not
pages of course but their owners) agreed to include my small script at
the begging (right after <bodytag, I can change architecture at this
moment, this is requirement) and do redirection if needed. [...]
I think there is your problem. Your script has to be loaded *in* the <body>
tag, in the `onload' attribute value of the `body' element instead:

<body onload="loadYourScript();">

That way it won't block loading the other content but will be triggered when
the rest of the content has finished loading. It is also the only way you
can be pretty sure that you actually can modify the document tree as
required (don't forget the feature tests at runtime, though). Including
scripts this way is still not reliable, though.

Please provide an attribution line above the quote next time. You are using
Google Groups, it should not be that hard to leave it in.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jun 27 '08 #10

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

Similar topics

11
2143
by: Sebastian Schöps | last post by:
Hello all, I have to access a website which does a stupid browser check and only accepts Netscape 4.7. The problem is that I have to access the website with Mozilla or Internet Explorer. ...
1
1327
by: Jeff Mowatt | last post by:
Hi all, A few months ago I was creating a small amount of revenue for a charitable effort from a referral program. Back in August it stopped producing results and only recently I began looking...
1
2189
by: zeeshan359 | last post by:
Hi, I need free script that is "date and time" picker in javascript without popup, It should be in iframe or div or layers. I shall be thankful to you, if you provide me some details of it....
4
2548
by: trpost | last post by:
I am looking for a script using javascript to pull browser information including, browser type and version, OS info, plugins (flash, acrobat, media player, etc), java version, etc. that will work...
3
1576
by: Sidanalism | last post by:
I miss the old way when asynchronous calls are driven my window messages. Yes a thread pool might greatly improve application performance but there are time I don't want my event to be fire in a...
1
2179
by: jatrojoomla | last post by:
Hi, I am New in JS world, I want's to submit a from using JavaScript ? is it possible? if yes then how? how to impliment document.form.submit? plz help me.
3
5579
by: vinay | last post by:
Hi All I have a page in which i using user control .on user control i using javascript to pop up a message .when i choose wrong date in user control & submits the page it alerts the message of...
11
15250
by: RC | last post by:
I wrote many JavaScripts, they can only run in a browser (Netscape, IE, Firefox, etc.) I am wonder how can I run the JavaScript in the command line (UNIX/LINUX, Windows)? Similar we can run Perl,...
2
6960
by: pavi | last post by:
Anyone know how to set the Default homepage using javascript without prompting a pop up. Below is the code i used to set default home page. <a href="#"...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7066
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
7504
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
5643
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,...
0
4724
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
3214
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
1568
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 ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.