0

I am using a javascript function to generate a random string:

function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

function guid() {

    /*$.ajax({
        type: "GET",
        url: "uuid.php",
        cache: false,
        success: function(html){
            return html;
        }
    });*/

   return (S4()+S4()+S4()+S4());
}

And I want to make it utilize a php uuid library that I've found, the problem is I need it to run in javascript. I use the guid() function a lot and I've been trying to think of an elegant way of grabbing the uuid, that I request using the ajax object (commented out above). The uuid page that just prints random uuids each time is sitting locally next to this page. I would not like to make the request synchronous because, like I said, I use it quite a bit and would prefer to not have everything halt every time this thing makes a request. Or perhaps there's a method I could use of jQuery that be as fast and not hinder performance?

I'm not adverse to changing things up a bit, like would the best practice here to acquire a uuid on load? But the number of UUIDs I generate is completely dynamic, and dependent upon the user.

Thank you!

2
  • What do you mean by "sitting locally next to this page"? The "next to" relationship doesn't make sense when applied to pages. Also, what are you using the GUID for? What PHP library are you using? Depending on the use, the technique of generating the GUID may not be suitable. Commented Aug 29, 2011 at 22:02
  • possible duplicate of How to create a GUID / UUID in Javascript? Commented Aug 29, 2011 at 22:29

2 Answers 2

2

Check the uniqid() function from phpjs.org.

Sign up to request clarification or add additional context in comments.

Comments

1

How about adding a callback argument to the guid() function, wherein you can assign a value to something:

function guid(callback) {
    $.ajax({
        type: "GET",
        url: "uuid.php",
        cache: false,
        success: function(html){
            callback(html);
        }
    });
}

var value;

guid(function (result) {
    value = result;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.