[RESOLVED] Javascript array.join ignoring specified delimiter
Hi,
Can anyone see why the below join isn't working? It insists on joining the string with a comma ",". It's not the "~" as I've tried changing the delimiter to dot "." and "|" with no effect and it's not the splice as I've commented it out and that didn't make any difference either
Code:
<html>
<body>
<script type="text/javascript">
var item=1;
var val="jani~hege~stale";
val=val.split("~");
if(val.length>=item){
val.splice(item,1);
val.join("~");
}
alert(val);
</script>
</body>
</html>
Any help will be greatly appreciated
Cheers Al
Re: Javascript array.join ignoring specified delimiter
What browser does it fail in? A command like this:
""jani~hege~stale".split("~").join("~")"
works fine in my Firefox 1.5. It also works in Opera. I was too lazy to test in Konqueror, which didn't like JavaScript being entered directly in the address bar.
Edit: OK, I know what your error is. Like split(), join() does not modify the object it is called on but instead returns the string it creates. You don't assign this string to anything. Thus what your alert displays is just the default stringification of the array, which uses commas.
Re: Javascript array.join ignoring specified delimiter
CornedBee,
Thanks for your reply, using ie7.
Haven't tested this, but I take it what you're saying is that I need to assign the join to a variable.
Cheers Al
Re: [RESOLVED] Javascript array.join ignoring specified delimiter