|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
I have problem with part of string extraction. I'm developing a moviecard for eXtreme movie manager in HTML format.
Here is my problem: I have a string variable "_MOVIE_GENRE_" in which can be entered for example "comedy/drama/action". I want to create script that for every genre will be inserted a gif pic. That i do so far: var genre="_MOVIE_GENRE_"; var comedy="<IMG src='moviecards/cortexmod/comedy.gif' border='0' align='absbottom' alt='comedy' width='25' high='12'>"; var drama="<IMG src='moviecards/cortexmod/drama.gif' border='0' align='absbottom' alt='drama' width='25' high='12'>"; switch(genre) { case "Comedy": document.all.genre.insertAdjacentHTML("AfterEnd",comedy); break; case "Drama": document.all.genre.insertAdjacentHTML("AfterEnd",drama); break; } . . . . Later in code i insert the gifs with: <genre id="genre"> That works fine if the string is "comedy" OR "drama" but i want to insert two gifs if string in the var "_MOVIE_GENRE_" is "comedy/drama" Any help will be welcome. Thanx you all! P.S. Forgive my english. |
|
#2
|
||||
|
||||
|
a more efficient way to write that might be:
Code:
var genre="_MOVIE_GENRE_";
var comedy="comedy.gif";
var drama="drama.gif";
document.all.genre.insertAdjacentHTML("AfterEnd","<IMG src='moviecards/cortexmod/"+comedy+"' border='0' align='absbottom' alt='comedy' width='25' height='12'>");
This stores less repetitive information in a variable. As for the parsing of a string, you might want to look at Javascript's indexOf() function in combination with the substr() function. Then just pop them all into an array and use Javascript's for (var i in object) (equivalent to Perl's foreach structure) to print the HTML |
|
#3
|
|||
|
|||
|
Another question
Thanx for quick reply.
Ive puted this together and i wonder if its ok: var genre="_MOVIE_GENRE_"; var comedy="comedy.gif"; var drama="drama.gif"; var comedy1=genre.indexOf("Comedy"); if (comedy1 >= 0) { document.all.genre.insertAdjacentHTML("AfterEnd","<IMG src='moviecards/cortexmod/"+comedy+" border='0' align='absbottom' alt='comedy' width='25' height='12'>"); } Thanx. |
|
#4
|
||||
|
||||
|
So long as it works, I guess its okay.
I was thinking something along the lines of searching for the index of the delimiting character, in your example it was a slash (/)... you can then form an array of the genres... where is genre being set? if you're setting it to "_MOVIE_GENRE_" then when would you ever find the word "Comedy"? |
|
#5
|
|||
|
|||
|
I did it
I finaly put together my code. I never thought it is so simple. Here is what i did:
var genre="_MOVIE_GENRE_"; var separator=" - " var comedy=genre.indexOf("Comedy"); if (comedy >= 0) { document.all.genre.insertAdjacentHTML("AfterEnd","<IMG src='moviecards/cortexmod/comedy.gif' border='0' align='absbottom' alt='Comedy' width='30' height='30'>"+ separator); } var thriller=genre.indexOf("Thriller"); if (thriller >= 0) { document.all.genre.insertAdjacentHTML("AfterEnd","<IMG src='moviecards/cortexmod/thriller.gif' border='0' align='absbottom' alt='Thriller' width='30' height='30'>"+ separator); } var action=genre.indexOf("Action"); if (action >= 0) { document.all.genre.insertAdjacentHTML("AfterEnd","<IMG src='moviecards/cortexmod/action.jpg' border='0' align='absbottom' alt='Action' width='30' height='30'>"+ separator); } . . . . and so on... Thanx for the tip. P.S.: "_MOVIE_GENRE_" is a tag which this program eXtreme movie manager imports a contents of a field named "MOVIE GENRE" and tag "_TMOVIE_GENRE_" imports name of that field. Brixie |
![]() |
| Viewing: Dev Articles Community Forums > Web Design > Web Development > Part of string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|