473,809 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

General function "template"

I would like to have a javasript/jscript function template/"framwork"
instead of checking browsers by name. The shortness of script in my opinion
is not the goal, but clearness and ease iof development. Could you give your
points, corrections and ideas about template below?

And are elemens ids available in form or another in all browsers or was
there some which accpets only element names? And was it that ancient
netscape only gives form elements, links, anchors and images available but
no other elements? In Finland some public organisations with lots of
machines unfortunately still have netscape 4.7.

function do_something(el em_id) // or should we other ways of referencing
too?
{
if(document.get ElementById() && e = document.getEle mentById(elem_i d))
//latest versions
{
if(e.needed_met hod()) / if(e.needed_pro perty) do_something_wi th_e
else if(e.alternativ e_method()) / if(e.alternativ e_property)
do_something_wi th_e
...
else return false;
}
elseif(document .all && e = document.all[elem_id]) // IE 5.5
{ do_something_wi th_e }
elseif( e = document.elem_i d) // Netscape 4.x
{ do_something_wi th_e_or_give_up _hope }
else return false;
}
Jul 23 '05 #1
1 1575
Perttu Pulkkinen wrote:
I would like to have a javasript/jscript function template/"framwork"
instead of checking browsers by name. The shortness of script in my opinion
is not the goal, but clearness and ease iof development. Could you give your
points, corrections and ideas about template below?

And are elemens ids available in form or another in all browsers or was
there some which accpets only element names? And was it that ancient
netscape only gives form elements, links, anchors and images available but
no other elements? In Finland some public organisations with lots of
machines unfortunately still have netscape 4.7.

function do_something(el em_id) // or should we other ways of referencing
too?
{
if(document.get ElementById() && e = document.getEle mentById(elem_i d))
Here you are testing the result of calling document.getEle mentById() without any
parameters, not testing if "getElementById " is a property of document. I think
what you want is:

if(document.get ElementById && e = document.getEle mentById(elem_i d))

Note no brackets.
//latest versions
{
if(e.needed_met hod()) / if(e.needed_pro perty) do_something_wi th_e
Again, you test for the existance of a property with:

if (e.needed_metho d) or if (typeof e.needed_method != 'undefined')
else if(e.alternativ e_method()) / if(e.alternativ e_property)
do_something_wi th_e
...
else return false;
}
elseif(document .all && e = document.all[elem_id]) // IE 5.5
{ do_something_wi th_e }
elseif( e = document.elem_i d) // Netscape 4.x
{ do_something_wi th_e_or_give_up _hope }
else return false;
}


There is no way to generate a "generic" template. Attempts have been made, but
the code quickly bloats due to the significant differences in the DOM of various
browsers. I prefer smaller atomic methods to perform the tasks I want in
predictable ways:

function setVisibility(i d, visible) {
var o;
if (document.getEl ementById) {
o = document.getEle mentById(id).st yle;
} else if (document.layer s) {
o = document.layers[id];
} else if (document.all) {
o = document.all[id].style;
}
if (o) {
o.visibility = (visible ? 'visible' : 'hidden');
}
}

or better yet:

if (document.getEl ementById) {
window.setVisib ility = function(id, visible) {
var o = document.getEle mentById(id);
if (o && o.style) {
o.style.visibil ity = (visible ? 'visible' : 'hidden');
return (o.style.visibi lity == (visible ? 'visible' : 'hidden'));
}
}
} else if (document.layer s) {
window.setVisib ility = function(id, visible) {
var o = document.layers[id];
if (o) {
o.visibility = (visible ? 'visible' : 'hidden');
return (o.visibility == (visible ? 'show' : 'hide');
}
}
} else if (document.all) {
window.setVisib ility = function(id, visible) {
var o = document.all[id];
if (o && o.style) {
o.style.visibil ity = (visible ? 'visible' : 'hidden');
return (o.style.visibi lity == (visible ? 'visible' : 'hidden'));
}
}
} else {
window.setVisib ility = function(id, visible) {
return false;
}
}

Then you just do:

if (setVisibility( 'myId', true)) {
// visibility *appears* to have been changed successfully
} else {
// visibility definitely wasn't changed
}

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq

Jul 23 '05 #2

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

Similar topics

3
1500
by: Griff | last post by:
#include <iostream> using namespace std; #include <vector> #include <string> #include <fstream> #include <algorithm> template<class C>void PrintAll(C&v) {
10
1785
by: Lionel B | last post by:
Greetings, I cannot figure out why the following code does not compile: --- BEGIN CODE --- typedef double ftype(double); struct A {
2
6197
by: Hartmut Sbosny | last post by:
Hello NG, I have a question. I have a header file with a function template and a fully specialized version of it, for instance //======== File "templ.hpp" ======== #include <iostream> // the general function template... template <class T>
2
2024
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since there are hundreds of them, this is quite obnoxious. Can I disable these things? Why is MSVC...
9
3352
by: Kobe | last post by:
Is there any difference in: template <class T> vs. template <typename T> ?
0
1964
by: Robbie Hatley | last post by:
I'd always thougth that a C++ compiler/linker should be able to instantiate a template in mulitple places (say, in two different translation units), even using the same template parameters so that the resulting functions or classes are identical, without causing any errors. Or so I thought. But a few days ago, I added a couple of new template functions to one of my personal library modules, and I got this linker error message (from...
1
1996
by: Robbie Hatley | last post by:
I asked about this yesterday, but no one bit. So I'll ask again. I can be a persistant cuss. :-) I ran into a problem a few days ago when I added a couple of template functions to one of my personal library headers. My library, librh.a, has these objects: rhutil.o rhdir.o rhmath.o rhbitmap.o and matching headers: rhutil.h rhdir.h rhmath.h rhbitmap.h
2
2806
by: Robbie Hatley | last post by:
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote: > Robbie Hatley wrote: > > > > I ran into a problem a few days ago when I added a couple of > > template functions to one of my personal library headers. > > > > My library, librh.a > > Linking is not defined by C++ .
3
1483
by: Aries Sun | last post by:
I am reading the book "Effective C++" Item 25 mentioned the following code: // a first cut at a class yielding NULL pointer objects class NullClass { public: template<class T // generates operator T*() const { return 0; } // operator T* for }; // all types T; each // function returns // the null pointer
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10387
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5550
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
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 we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.