473,398 Members | 2,335 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,398 software developers and data experts.

Is this common?

Hi all,

I came across some code on a big-name site, that I dont like, but need
to support (I am writing a DOM model for a product).

Here is the code:

var d=document.writeln;
d("<HTMLSTUFF>.....");

Now, this works in IE, but does not work in Netscape. Netscape will
throw an exception. The authors only did this in IE (using browser
detection), but it is just plain bad, in my opinion.

The variable d is a function object. It is not necesarially associated
with document. In fact, in the Mozilla implementaiton of javascript,
this object would be executed from the global (window) object.

My question, is: "Is this a common thing to do?" Do people do this
often? In what other contexts do they do this?

I am in no way advocating this type of code... nor do I plan to use it.
I need to support it, which is whay I ask.

Just so anyone is curious, a MUCH better solution would be:

function d(text) { document.writeln(text) }
d("<HTMLSTUFF>.....");

Ok, Thanks,
Brian

Jul 20 '05 #1
5 1210
Brian Genisio wrote:
var d=document.writeln;
d("<HTMLSTUFF>....."); Now, this works in IE, but does not work in Netscape. Netscape will
throw an exception. The authors only did this in IE (using browser
detection), but it is just plain bad, in my opinion.
Not only is testing only in IE quite awkward, but doing some browser
detection as well is a definitively bad approach.
The variable d is a function object. It is not necesarially associated
with document. In fact, in the Mozilla implementaiton of javascript,
this object would be executed from the global (window) object.
That's probably correct, but where did you see that?
My question, is: "Is this a common thing to do?" Do people do this
often? In what other contexts do they do this?
This depends on whether the object is a host objet or a js object.

Assuming it's a host object, then the answer is a definite NO, for the
reasons you've pointed out above. Hosts' objects implementation isn't
specified anywhere and differ effectively.

Assuming it's a js object, then the answer would be NO most of the time,
but not always. In js, you can indeed set the "this" value, using
Function.prototype.call or Function.prototype.apply, so having a
function without a context isn't a real problem, you can set a different
context each time you call the function - it depends on which problem
you're trying to solve.

Gecko's hosts object seem to be designed (and can be used) like js
objects, with a prototype and a "this" value. This means you can use the
generic js methods on them, like Array.prototype.join on any HTML
collection. But that also means you cannot use IE-like shortcut for the
function (thie "this" value being required).
Just so anyone is curious, a MUCH better solution would be:

function d(text) { document.writeln(text) }
d("<HTMLSTUFF>.....");


Which is what I would have suggested.
Regards,
Yep.
Jul 20 '05 #2
On Fri, 05 Mar 2004 12:37:32 -0500, Brian Genisio
<Br**********@yahoo.com> wrote:
Here is the code:

var d=document.writeln;
d("<HTMLSTUFF>.....");

Now, this works in IE, but does not work in Netscape.
It's a speed optimisation, worth a reasonable amount in IE.
Just so anyone is curious, a MUCH better solution would be:

function d(text) { document.writeln(text) }
d("<HTMLSTUFF>.....");


No, that would be a bad solution, since it just adds extra indirection
slowing things down even further.

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #3
Jim Ley wrote:
On Fri, 05 Mar 2004 12:37:32 -0500, Brian Genisio
<Br**********@yahoo.com> wrote:

Here is the code:

var d=document.writeln;
d("<HTMLSTUFF>.....");

Now, this works in IE, but does not work in Netscape.

It's a speed optimisation, worth a reasonable amount in IE.

Just so anyone is curious, a MUCH better solution would be:

function d(text) { document.writeln(text) }
d("<HTMLSTUFF>.....");

No, that would be a bad solution, since it just adds extra indirection
slowing things down even further.

Jim.


I disagree. The first option is only availble in IE as far as I know.
At least, it is not available in Mozilla. There is no way of reliably
detecting if a browser will support this optimization method. The
second option will work in all browsers that support document.writeln.

Portablity is much more important than speed, in a massively viewed site.

Brian

Jul 20 '05 #4
Jim Ley wrote:
It's a speed optimisation, worth a reasonable amount in IE.
That makes sense, from an implementation point of view; still, the
amusing following test gives surprising results (the shorthand function
is *four* times slower than a regular identifier resolution):
<div id="bar"></div>
<script type="text/javascript">
window.onload=function(){
var d=document, gebi=d.getElementById, foo;
var d1, d2, d3;

d1=new Date();
for(var ii=0; ii<10000; ii++) {
foo=d.getElementById("bar");
}
d2=new Date();
for(var j=0; j<10000; j++) {
foo=gebi("bar");
}
d3=new Date();

alert((d2-d1)+"\n"+(d3-d2));
}
</script>

gives 791 for the first method, and 3405 for the second one.
No, that would be a bad solution, since it just adds extra indirection
slowing things down even further.


To me it's not a matter of speed, but if speed matters then it'd be
better to buffer the output and write it only when required...
Regards,
Yep.
Jul 20 '05 #5
On Fri, 05 Mar 2004 14:11:14 -0500, Brian Genisio
<Br**********@yahoo.com> wrote:
Jim Ley wrote:
function d(text) { document.writeln(text) }
d("<HTMLSTUFF>.....");

No, that would be a bad solution, since it just adds extra indirection
slowing things down even further.

Jim.


I disagree. The first option is only availble in IE as far as I know.
At least, it is not available in Mozilla. There is no way of reliably
detecting if a browser will support this optimization method.


Sorry, I seem to put that badly, my point was not doing it at all and
just using document.writeln would be appropriate, not using the
caching method of IE.

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #6

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

Similar topics

23
by: George | last post by:
Is there a way to customize the open file common dialog? I am trying to modify the button text so I can create a delete file common dialog. I need the same functionality of the open file common...
5
by: wrecker | last post by:
Hi all, I have a few common methods that I need to use at different points in my web application. I'm wondering where the best place would be to put these? I think that I have three options. ...
6
by: Steve Barnett | last post by:
I need to include a wizard in my application that will, as one of the steps, ask the user to select a file to open and (later) a file to save it as. The naff way to do this would be to have a...
6
by: Peter | last post by:
Hi I have a number of arrays of longs, from which I need to find a single array which only contains the values which appear in all the original arrays. For example, I could have the three...
6
Markus
by: Markus | last post by:
Things to discuss: Headers What are they? What does PHP have to do with headers? Why can they only be sent before any output? Common causes
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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,...
0
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...

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.