4

I'm converting a guid and then I'm converting it in base 16 with parseInt(), then I display it to generate a short guid.

It's working fine in Chrome and FireFox, but not in IE.

Here's the code:

var guid32 = function () {
    "use strict";
    return "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c === "x" ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
};

var shortGuid = function () {
    "use strict";
    return parseInt(guid32(),16).toString(36);
};


for(var i =0; i < 100; i++)
{
    $("#guids").append("<p>"+ shortGuid() +"</p>")
}

Here's the implementation of guid32().

Here's the fiddle to test: https://jsfiddle.net/domwu51r/

Why is it different in IE and how can I solve it?

I'm guessing that the implementation in IE of toString() is different of Chrome and FireFox.

How can I keep the generated guid whitout the scientific notation?

------------------------------EDIT---------------------------------

The other given answer does not help me solve the problem. I'm already using toString(36), so it's not the good answer for my question. Also, it's not explainning why it's happening only in IE and not in the other browsers.

Community
  • 1
  • 1
billybob
  • 2,859
  • 6
  • 35
  • 55
  • 1
    1) what does "not working" mean?; 2) Without knowing the details about GUIDs - I personally doubt that the thing you generate is a "GUID". You simply use the Javascript Math.random function. How are the numbers guaranteed or at least "very likely" to be "globally unique"? – chiccodoro Nov 26 '15 at 14:59
  • 2
    1) Have you ran the fiddle in the 3 browsers? It's not giving the expect result such as chrome and firefox 2) I generated 1 million short guid and it's not colliding, so it's good enought for what I need... – billybob Nov 26 '15 at 15:10
  • 1
    This will answer your question : http://stackoverflow.com/questions/32625702/cross-browser-random-string-math-random1e32-tostring36 – Stranded Kid Nov 26 '15 at 15:30
  • Yeah, but it's exactly what I'm doing, I'm using toString(36). It's not working only in IE. – billybob Nov 26 '15 at 15:38
  • Duplicate to http://stackoverflow.com/questions/1685680/how-to-avoid-scientific-notation-for-large-numbers-in-javascript – Werner Henze Nov 26 '15 at 16:48

0 Answers0