April 6, 2012

doesn't check for me?

Question by Fightpa Sirprite

i use split function

string = data.con_loop[i].split("\.");

string input is “0.9.L” when i check

if(string[2]=="L")

it doesn’t into statement in scope of if Why?

Answer by Starx

You don’t need double slashes, then use equals to check

string = data.con_loop[i].split(".");
if(string[2].equals("L")) {
    //.....
}
March 23, 2012

Javascript – Cannot call method 'split' of null

Question by Lelly

I have a javascript error that occurs on my site, Im pretty sure I know why, but I dont know how to fix it!

here’s the error:

Uncaught TypeError: Cannot call method 'split' of null

Here’s my JS code:

$(function(e) {
    if (document.cookie.indexOf("login") >= 0) {
        $("a#loggedInUser").html( $.cookie("login").split("|")[0] );
    }
});

I’m just trying to display the username stored in the “login” cookie. Now, im pretty sure the error is because the value returned sometimes isn’t a string, then it doesn’t have the split method, so it causes this error.

How can I fix that? Any ideas?

Thanks!

Answer by Jamund Ferguson

Well you can do something like this to set a default if the value is null.

var login = $.cookie("login") || "";
$("a#loggedInUser").html(login);

Also, if you do that you might not need the document.cookie.indexOf thing.

Answer by Starx

Check the length of the cookie. This way you validate two things at once.

if (document.cookie.indexOf("login") >= 0 && $.cookie("login").length) {
    $("a#loggedInUser").html( $.cookie("login").split("|")[0] );
}
March 11, 2012

Better way of splitting and assigning many values in Javascript?

Question by Aaron

I have a for loop that cycles through the number of elements that the user has created. There are a lot of available settings in this plugin, and each element can receive it’s specific settings.

  1. User settings are entered in the following format: speed_x: “1000,500 > 1000,200 > 0,0”
    This controls the speed_x in/out for 3 separate elements. The > divides by object and the commas separate the in/out.

  2. So I can grab specific object speed_x values, I’ve split speed_x into speed_x_set (splitting by >) resulting in:

1 1000,500
2 1000,200
3 0,0`

3 Inside the loop, I grab the value by index (since it’s the object #) and split it by comma (to get speed_x_in and speed_x_out.)

for(var i=0; i<OS.numberofobjects; ++i){
   OS.speed_x_on_set[i]=speed_x_set[i].split(",")[0],
   OS.speed_x_off_set[i]=speed_x_set[i].split(",")[1],
   ...
};

Everything is assigned by object and by setting in/out correctly into the master OS settings object. T*he problem is I have many, many settings which need to be split in this fashion…* for example: delay_x_set, speed_y_set, opacity_set, etc. Their names are all based on the default setting name, with “_set” added as shown above. Hopefully this provides enough information. Thanks!

Answer by Starx

I would say to cache the split result

for(var objindex=0; objindex<OS.numberofobjects; ++objindex){
   var splits = speed_x_set[objindex].split(","); //Cache the split so its does not need to be done twice
   OS.speed_x_on_set[objindex] = splits[0];
   OS.speed_x_off_set[objindex] = splits[1];
   ...
};
July 15, 2010

Split a string array into pieces

Question by arkchong

let said I have an array that store like this.

Array ( 
   [0] => width: 650px;border: 1px solid #000; 
   [1] => width: 100%;background: white; 
   [2] => width: 100%;background: black; 
) 

How should I make the array[0] string split into piece by separated the “;”? Then I want to save them in array again, or display them out. How should I do it?

Array(
   [0] => width: 650px
   [1] => border: 1px solid #000
)

Any idea? Thank in advanced

Answer by animuson

I would personally use the preg_split to get rid of that extra array element that would occur from the final semicolon…

$newarray = array();
foreach ($array as $i => $styles):
    // Split the statement by any semicolons, no empty values in the array
    $styles = preg_split("/;/", $styles, -1, PREG_SPLIT_NO_EMPTY);
    // Add the semicolon back onto each part
    foreach ($styles as $j => $style) $styles[$j] .= ";";
    // Store those styles in a new array
    $newarray[$i] = $styles;
endforeach;

Edit: Don’t add the semicolon to each line:

$newarray = array();
foreach ($array as $i => $styles):
    // Split the statement by any semicolons, no empty values in the array
    $newarray[$i] = preg_split("/;/", $styles, -1, PREG_SPLIT_NO_EMPTY);
endforeach;

Which should output:

Array(
   [0] => width: 650px;
   [1] => border: 1px solid #000;
)

Unlike explode, which should output:

Array(
   [0] => width: 650px;
   [1] => border: 1px solid #000;
   [2] => ;
)

Answer by Starx

An example

foreach($array as $item) {
   $mynewarray = explode(";",$item);
   foreach($mynewarray as $newitem) {
        $finalarray[] = $newitem.";";
   }
   //array is ready
}
...

Please fill the form - I will response as fast as I can!