JAVASCRIPT - dateobj.getTime for a UID - Is the length not fixed? - javascript

I'm using the following 2 lines of JS to create a UID:
var UID = dateobj.getTime();
UID = String(UID).substr(4);
It appears that sometimes it generates a number like:
564929300
other times like:
56492930
Problem is the length isn't consistent which is messing things up. Any ideas how that's possible and if there is a way to fix this or a better way to make a UID with JS?
Thanks

I like doing Math.random().toString(36).substr(2,9)

There one implementation here:
http://blog.shkedy.com/2007/01/createing-guids-with-client-side.html
Here it is in action: http://jsfiddle.net/7sXL6/
I threw together a smaller version of it: http://jsfiddle.net/7sXL6/4/

Related

Unable to convert strings to proper integers in Javascript

So I am trying to convert a string to an integer in javascript and then comparing it to an integer variable I created. I have read many many things online but nothing worked so far. This is my code:
var followedUsers = $('.ProfileNav-item.ProfileNav-item--following.is-active').find('.ProfileNav-value').html();
so when I type followedUsers in console I get "1256". I tried parseInt as follows:
var x = parseInt(followedUsers,10);
But when I type x in console I get 2. Yes it is an integer but its not correct integer.
I also tried parseFloat. It gives the same result. This one didn't work either.
var x=parseInt(followedUsers.valueOf(),10);
I dont know if this is a thing but I was just messing around trying different things. I saw another guy's question. So I gave that a shot:
var x = +followedUsers
This one gave me a NaN.
var x = followedUsers*1
This one also gave me a NaN.
I tried converting my integer value to a string and comparing them that way but that doesn't gives me correct outputs. I am running out of ideas guys, am I missing something here? Changing a string to an integer shouldn't be this hard, right?
Perhaps followedUsers isn't what you think it is.
I made a simple example in JSBin, trying to simulate you problem and everything is working:
http://jsbin.com/huzegovaxa/edit?html,js,console,output
Try getting followedUsers with .val() insted of .html()
It turns out that the old browser in my university computer lab doesn't support parseInt or any other methods I mentioned above. It worked fine when I tried it on other computers. Although I believe this post will be useful for anybody looking for conversion in means of seeing all the methods in one place
Try
var x = parseInt(followedUsers);

How to Split many Strings with Jquery

Let's say I have a bunch of strings which I get from user input. Each strings starts with a new line like the following example:
gordon:davis
harley:vona
empir:domen
furno:shyko
I would like to write a function in jQuery which loads the user data from the form and splits each row's sting. However I would ONLY need the first part of the strings, like:
gordon
harley
empir
furno
Is there any simple solution for this?
I have found something called: $.each(split) but I didn't get it work.
I'm sorry I'm a really newbie in Jquery, I hope you guys can help me out! Thanks in advance!
Use String.prototype.split method.
"gordon:davis".split(":") will return ["gordon","davis"]
Based on this, using Array.prototype.map or a JQuery version :
var strings = ["gordon:davis", "harley:vona"];
var splitStrings = strings.map(function (string) {
return string.split(":")[0];
});

Adapt textarea max word plugin

How would one adapt this jquery plugin so that it counts down from how many words your allowed/you have remaining, instead of counting up to how many your allowed.
www.javascriptkit.com/script/script2/enforceform.shtml
Thanks in advance.
There is also a very simple way to do it without a maxlength attribute. (This example uses 200 as the maximum characters).
$("#YourTextareaId").keyup(function () {
var i = $("#YourTextareaId").val().length;
$('#IdOfCountdownDisplay').val(''+200-i+'');
A nice thing to remember when coding, is that a simple equation can get rid of a whole lot of complicated code.
open the maxlength.js file and put this
$field.data('$statusdiv').css('color', '').html( $field.data('maxsize') - $field.val().length )
instead of this:
$field.data('$statusdiv').css('color', '').html($field.val().length)

Javascript - GetColoumnValue - IE6 problem

I have a js function named "GetListColumnValue". This function causes some problems with IE6. Is there any way to avoid the problem? (I thnk the problem is occured because of the concat) Here is the code sample. The last line is my solution which I am not sure that it works well. Any suggestions? Thanks.
function GetListColumnValue(listName, columnName) {
return document.getElementById(listName + "_" + columnName).value;
}
var DISCOUNT_QUANTITY = GetListColumnValue("lstRecords", "DISCOUNT_QUANTITY");
var DISCOUNT_QUANTITY = document.getElementById("lstRecords_DISCOUNT_QUANTITY");
IE6 has many many problems, but simple JS string concat isn't one of them. I don't think that's your problem.
You didn't specify what exactly the problem is, but looking at the two code samples you provided, they will do different things:
The first one (ie the function) returns the object.value, whereas the second one (ie setting it directly), you've just returned the object.
So the two code blocks set DISCOUNT_QUANTITY to different things. If you remove the .value from the function, it should work exactly the same as the other code block.
Hope that helps.

How to increase speed of getElementById() function on IE or give other solutions?

I have a project using Javascript parse json string and put data into div content.
In this case, all of itemname variables is the same div's id.
If variable i about 900, I run this code in Firefox 3 for <10ms, but it run on IE 7 for >9s, IE process this code slower 100 times than Firefox
I don't know what happen with IE ?
If I remove the line document.getElementById(itemname), speed of them seems the same.
The main problem arcording to me is document.getElementById() function?
Could you show me how to solve this prolem to increase this code on IE ?
Thank in advance.
var i = obj.items.length-2;
hnxmessageid = obj.items[i+1].v;
do{
itemname = obj.items[i].n;
itemvalue = obj.items[i].v;
document.getElementByid(itemname);
i--;
}while(i>=0);
Are you really noticing any latency?
gEBI is natively very very fast, I don't think you can avoid it anyway for what you're doing. Could you provide a low-down of what you're doing precisely? It looks like you're using a loop, but can you post exactly what you're doing inside of the loop, what your common goal of the script is?
document.getElementByid(itemname) is the fastest way to get a single element from the DOM in any real application you will sould not see any problems with using it, if you do see a problem you need to rethink you code a little it possible to acomplish any task with just a handfull of calls for this method. You can present you full problem if you like so I could show you an example
At least cache the reference to document:
var doc = document;
for(;;) {
doc.getElementById(..);
}

Categories

Resources