PDA

View Full Version : How can I call this javascript, externally?



pdark
12-20-2004, 05:08 PM
How can I call this javascript, externally? (Random-Order content script)

Step 1

<script>

/*
Random-Order content script
By JavaScript Kit (http://www.javascriptkit.com)
This notice must stay intact for use
*/

//1) Specify content(s) to display and rotate
// You can specify multiple "sets", each displayed in diff. areas of your page

var content=new Array() //sample set 1
content[0]='<a href="http://www.dynamicdrive.com"><img src="http://www.javascriptkit.com/dynamic.gif" border=0></a>'
content[1]='<a href="http://cooltext.com"><img src="http://www.cooltext.com/images/ctb4.gif" border=0></a>'
content[2]='<a href="http://thefreesite.com"><img src="http://www.thefreesite.com/free88.gif" border=0></a>'

var diffcontent=new Array() //sample set 2
diffcontent[0]='<a href="http://www.google.com">Google Search Engine</a>'
diffcontent[1]='<a href="http://news.bbc.co.uk">BBC News</a>'
diffcontent[2]='<a href="http://www.msnbc.com">MSNBC News</a>'


function randomorder(targetarray, spacing){

var randomorder=new Array()
var the_one
var z=0
for (i=0;i<targetarray.length;i++)
randomorder[i]=i

while (z<targetarray.length){
the_one=Math.floor(Math.random()*targetarray.length)
if (targetarray[the_one]!="_selected!"){
document.write(targetarray[the_one]+spacing)
targetarray[the_one]="_selected!"
z++
}
}
}

</script>

step 2

<script>
//2) Call function randomorder(arrayname, space_between_content)
randomorder(content, '<br><br>')
randomorder(diffcontent, '<br>')
</script>


Ok... I know that in order to call this from a external file you take... step 1, and remove the end tags, then save n' name it to a .js file. Thats the easy part... but how do I call the .js if theres step's 2 coding left... like this?

<script src="name_of_file.js">
randomorder(content, '<br><br>')
randomorder(diffcontent, '<br>')
</script>

usually all you have to do is this...

<script src="name_of_file.js"></script>

and your fine, but the remaining code...

randomorder(content, '<br><br>')
randomorder(diffcontent, '<br>')

keeps me from doing so...Any idea's how to call it?

Willy Duitt
12-20-2004, 06:43 PM
Did you try placing the script itself in an external file and leave those step two's alone???

requestcode
12-20-2004, 07:47 PM
You could place them both in the same script file like so:
/*
Random-Order content script
By JavaScript Kit (http://www.javascriptkit.com)
This notice must stay intact for use
*/

//1) Specify content(s) to display and rotate
// You can specify multiple "sets", each displayed in diff. areas of your page

var content=new Array() //sample set 1
content[0]='<a href="http://www.dynamicdrive.com"><img src="http://www.javascriptkit.com/dynamic.gif" border=0></a>'
content[1]='<a href="http://cooltext.com"><img src="http://www.cooltext.com/images/ctb4.gif" border=0></a>'
content[2]='<a href="http://thefreesite.com"><img src="http://www.thefreesite.com/free88.gif" border=0></a>'

var diffcontent=new Array() //sample set 2
diffcontent[0]='<a href="http://www.google.com">Google Search Engine</a>'
diffcontent[1]='<a href="http://news.bbc.co.uk">BBC News</a>'
diffcontent[2]='<a href="http://www.msnbc.com">MSNBC News</a>'


function randomorder(targetarray, spacing){

var randomorder=new Array()
var the_one
var z=0
for (i=0;i<targetarray.length;i++)
randomorder[i]=i

while (z<targetarray.length){
the_one=Math.floor(Math.random()*targetarray.length)
if (targetarray[the_one]!="_selected!"){
document.write(targetarray[the_one]+spacing)
targetarray[the_one]="_selected!"
z++
}
}
}
randomorder(content, '<br><br>')
randomorder(diffcontent, '<br>')

Then just refer to it this way:
<script type="javascript/text" src="name_of_file.js"></script>

pdark
12-20-2004, 11:49 PM
Thanks requestcode,

but that didn't work...calling the file by using:

<script type="javascript/text" src="name_of_file.js"></script>

but, I got it to work by removing the:

type="javascript/text"

and called it using:

<script src="name_of_file.js"></script>

liorean
12-21-2004, 12:13 AM
Heh. He got it the wrong way- it should be 'text/javascript'. Oh, and the type attribute is required, so don't remove it, just correct it.

pdark
12-21-2004, 12:27 AM
Thanks liorean, will do.

jamesplato
08-16-2006, 11:34 PM
I know I am late to the party on this one. I have the script to work fine in mozilla. I keep getting an error as undefined in IE. Help is greatly appreciated, as I cannot seem to figure it out.

I should add error seems to come at randomorder(diffcontent, '<br>')
I don't why. I have tried everything.

Figured it out...please disregard me...
make sure you are careful if you decide to go with images or text only.