MAC addresses in JavaScript

MAC addresses - JavaScript

I know that we can get the MAC address of a user via IE (ActiveX objects).

Is there a way to obtain a user's MAC address using JavaScript?

8 Answers

I concur with all the previous answers that it would be a privacy/security vulnerability if you would be able to do this directly from Javascript. There are two things I can think of:

  • Using Java (with a signed applet)
  • Using signed Javascript, which in FF (and Mozilla in general) gets higher privileges than normal JS (but it is fairly complicated to set up)

I was courious to know, can we get a unique information like mac adress or serial number or something like that from user via JavaScript? Or PC name?

The quick and simple answer is No. Javascript is quite a high level language and does not have access to this sort of information.

then what about all the google search result providing sample codes to get MAC Address

I've had a quick look around Google and all of the pages I read were IE only solution which relied on using ActiveX objects. If you could post a link showing otherwise...

I really don't think being a "high level language" has anything to do with MAC addresses, since any server-side programming language allows you do get access the ARP table, even indirectly (e.g. through a subprocess). I think "client-side language" would work better...

There is no reason why an high level language wouldn't have access to low level hardware information. In this case this doesn't happen because it would be a security problem.

No you cannot get the MAC address in JavaScript, mainly because the MAC address uniquely identifies the running computer so it would be a security vulnerability.

Now if all you need is a unique identifier, I suggest you create one yourself using some cryptographic algorithm and store it in a cookie.

If you really need to know the MAC address of the computer AND you are developing for internal applications, then I suggest you use an external component to do that: ActiveX for IE, XPCOM for Firefox (installed as an extension).

Do you know of an external component for Safari?

No. I was actually suggesting to create one.

how are you suggesting to create a unique identifier? how do you use a cryptographic algorithm to guarantee that ever user that runs it on their machine gets a unique id?

Wikipedia lists several UUID implementations

If this is for an intranet application and all of the clients use DHCP, you can query the DHCP server for the MAC address for a given IP address.

Nope. The reason ActiveX can do it is because ActiveX is a little application that runs on the client's machine. I would imagine access to such information via JavaScript would be a security vulnerability.

i was looking for the same problem and stumbled upon the following code.

How to get Client MAC address(Web):

To get the client MAC address only way we can rely on JavaScript and Active X control of Microsoft. It is only work in IE if Active X enable for IE. As the ActiveXObject is not available with the Firefox, its not working with the firefox and is working fine in IE.

This script is for IE only:

function showMacAddress() {
    var obj = new ActiveXObject("WbemScripting.SWbemLocator");
    var s = obj.ConnectServer(".");
    var properties = s.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
    var e = new Enumerator(properties);
    var output;
    output = '<table border="0" cellPadding="5px" cellSpacing="1px" bgColor="#CCCCCC">';
    output = output + '<tr bgColor="#EAEAEA"><td>Caption</td><td>MACAddress</td></tr>';
    while (!e.atEnd()) {
        e.moveNext();
        var p = e.item();
        if (!p) continue;
        output = output + '<tr bgColor="#FFFFFF">';
        output = output + '<td>' + p.Caption; +'</td>';
        output = output + '<td>' + p.MACAddress + '</td>';
        output = output + '</tr>';
    }
    output = output + '</table>';
    document.getElementById("box").innerHTML = output;
}

showMacAddress();

<div id='box'></div>

I know I am really late to this party. And although the answer is still no. I found a way to generate and store a unique id that helps to keep track of a user while they navigate the site.

When the user signs up, I then have a full record of what pages he visited before he signed up. I also store this id in the user table for historical reference.

This is also handy when you're looking for dubious activity. For example, a user that has created multiple accounts. I should note that this is on a financial transaction site and the data is only used internally. It does really help to cut down on fraudulent and duped accounts. There is a lot that can be done with localStorage and this method, but I will keep this short to not give anyone nefarious ideas.

1- Generate a random string. If you generate a 40 char string, you don't really have too much to worry about as far as them colliding. We're not looking to send a rocket to Mars here.

I use a small PHP function to generate a 40 char string. I use Ajax to call this function and retrieve the result.

 function genrandstr($length=NULL) {
        if($length == NULL){ $length = 30; }
        $characters = 
        '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
        return $randomString;
    }

    localStorage.setItem('id', id) to add to localStorage
    (let) or (var) id = localStorage.getItem('id') to read from localStorage

2- Using cookies, you can store this id in a cookie.

3- LocalStorage is your friend. Store the same id in LocalStorage and chances are, you will always have that id to look at.

With an id stored in LocalStorage, your user can delete cookies and you'd still be able to recognize an old visitor by this id.

If the user deletes all their data, then you're SOL and start all over again when they revisit.

Have Fun

browser fingerprinting is better then

Maybe add an example of how you perform browser fingerprinting for us less enlightened folks? Your answer fails at any kind of information.

No you can't obtain a user's MAC address using JavaScript in another way, just by using active X op for Microsoft in IE browser

This has already been mentioned in many of the other answers.

Capturing TAB key in text box

Capturing TAB key

I would like to be able to use the Tab key within a text box to tab over four spaces. The way it is now, the Tab key jumps my cursor to the next input. Is there some JavaScript that will capture the Tab key in the text box before it bubbles up to the UI? I understand some browsers (i.e. FireFox) may not allow this. How about a custom key-combo like Shift+Tab, or Ctrl+Q? Don't forget to check for the focused window, and let it bubble up normally if you are not in the editor textarea 6 Answers Even if you capture the keydown/keyup event, those are the only events that the tab key fires, you still need some way to prevent the default action, moving to the next item in the tab order, from occurring. In Firefox you can call the preventDefault() method on the event object passed to your event handler. In IE, you have to return false from the event handle. The JQuery library provides a preventDefault method on its event object that works in IE a…

Read more…

How to set background color of HTML element using css properties in JS

How to set background color of HTML element

How can I set the background color of an HTML element using css in JavaScript? 17 Answers In general, CSS properties are converted to JavaScript by making them camelCase without any dashes. So background-color becomes backgroundColor . function setColor(element, color) { element.style.backgroundColor = color; } // where el is the concerned element var el = document.getElementById('elementId'); setColor(el, 'green'); I'd like to add the color obviously needs to be in quotes element.style.backgroundColor = "color"; for example - element.style.backgroundColor = "orange"; excellent answer In Selenium…

Read more…

How can I make the browser see CSS and Javascript changes?

How see CSS and Javascript changes

CSS and Javascript files don't change very often, so I want them to be cached by the web browser. But I also want the web browser to see changes made to these files without requiring the user to clear their browser cache. Also want a solution that works well with a version control system such as Subversion. Some solutions I have seen involve adding a version number to the end of the file in the form of a query string. Could use the SVN revision…

Read more…