Jump to content

Cross browser getById function


bennyboywonder

Recommended Posts

I am trying to make a cross-browser getById and getStyle function to use with my other javascripts;

 

at present to use these in conjunction with each other, I would have to write it like this

 

var mystyle = getStyle(getById("addcat"))

I think

function getById(whichLayer)
{
    this.getStyle = getStyle;
    object = document.getElementById ? document.getElementById(whichLayer)
           : document.all ? document.all[whichLayer]
           : document.layers ? document.layers[whichLayer]
           : false;
    return object
}
function getStyle(obj)
{
    var object = obj.style ? obj.style : obj;
    return object;
}

is their a way of writing an object so that I could use standard dot syntax, ie

var mystyle = getById("addcat").getStyle;

 

I was thinking something along the lines of

var browsers = new browserObject();

var mystyle = browsers.getById("myid").style;
mystyle.left = "10";
function browserObject()
{
    this.getById = function(whichLayer)
    {
        this.style = getStyle;
        object = document.getElementById ? document.getElementById(whichLayer)
               : document.all ? document.all[whichLayer]
               : document.layers ? document.layers[whichLayer]
               : false;
        return object
    }
}
function getStyle(obj)
{
    var object = obj.style ? obj.style : obj;
    return object;
}

 

though this is just a guess of the sort of thing I am after, and I know this doesn't work.

Link to comment
https://forums.phpfreaks.com/topic/41294-cross-browser-getbyid-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.