/    Sign up×
Community /Pin to ProfileBookmark

I have never written a script, we’ll get that out of the way now… ?

I have downloaded a script and made some modifications to suit my needs. I am wondering, as I am an amateur, if there is a more efficient way of accomplishing this. This script works and does everything I need it to do, but I am just curious.

Its purpose is to randomly select a song, embed it in the page and display the song title:

[CODE]
<!–
//By George Chiang (http://www.abstract.simplenet.com) More JavaScripts here!
//Modified by Jay Stead (http://www.stead.ca)
var title
var sound1=”/music/PavementCracks.asf”
var sound1_title=”Annie Lennox – Pavement Cracks”
var sound2=”/music/nottheone.asf”
var sound2_title=”Daniel Bedingfield – If You’re Not The One”
var sound3=”/music/onahigh.asf”
var sound3_title=”Duncan Sheik – On a High”
var sound4=”/music/eachday.asf”
var sound4_title=”Ronan Keating – Lovin’ Each Day”
var sound5=”/music/boysofsummer.asf”
var sound5_title=”DJ Sammy – The Boys of Summer”
var x=Math.round(Math.random()*4)
if (x==0) x=sound1
else if (x==1) x=sound2
else if (x==2) x=sound3
else if (x==3) x=sound4
else x=sound5
document.write(‘<embed src=”‘+x+'”hidden=”true” autostart=”true” nosave=”true”>’)

if (x == sound1) document.write(‘<small class=”min”>You are listening to: ‘+sound1_title+'</small>’)
else if (x == sound2) document.write(‘<small class=”min”>You are listening to: ‘+sound2_title+'</small>’)
else if (x == sound3) document.write(‘<small class=”min”>You are listening to: ‘+sound3_title+'</small>’)
else if (x == sound4) document.write(‘<small class=”min”>You are listening to: ‘+sound4_title+'</small>’)
else if (x == sound5) document.write(‘<small class=”min”>You are listening to: ‘+sound5_title+'</small>’)
//–>
[/CODE]

Any help appreciated!
Jay

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@xataku_nakusuteJul 28.2003 — by "efficient", what exactly are you implying?
Copy linkTweet thisAlerts:
@Khalid_AliJul 28.2003 — if u used array , you can reduce the code t almost half th esize,and hence the improvement in efficiency as well,however in this script you may not even notice much of difference in efficiency,you can definitely reduce the number of lines of code to half....
Copy linkTweet thisAlerts:
@CharlesJul 28.2003 — [font=monospace]<script type="text/javascript">

<!--

Array.prototype.random = function () {return this[Math.round (Math.random() * Math.ceil (this.length / 10) * 10) % this.length]}

var sound = new Array()

sound[0] = {href:'/music/PavementCracks.asf', title:'Annie Lennox - Pavement Cracks'}

sound[1] = {href:'/music/nottheone.asf', title:'Daniel Bedingfield - If You're Not The One'}

sound[2] = {href:'/music/onahigh.asf', title:'Duncan Sheik - On a High'}

sound[3] = {href:'/music/eachday.asf', title:'Ronan Keating - Lovin' Each Day'}

sound[4] = {href:'/music/boysofsummer.asf', title:'DJ Sammy - The Boys of Summer'}

var s = sound.random();

document.write('<embed src="', s.href, '"hidden="true" autostart="true" nosave="true">')

document.write('<small class="min">You are listening to: ', s.title, '</small>')

// -->

</script>[/font]
Copy linkTweet thisAlerts:
@jaysteadauthorJul 28.2003 — Well that is definitely much more efficient! ?

I only had to change one thing. A lot of my song titles have apostrophes (') in them which caused errors in the script, so I change the quotations from ' to " which seemed to work. Now I can have apostrophes (') in my song titles.

Thank you so much!

Jay
Copy linkTweet thisAlerts:
@steelersfan88Jul 28.2004 — if you have ' in your song title, you could either change the quotes, or escape the apostrophe (')
×

Success!

Help @jaystead spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...