Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How can I call this javascript, externally?

    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?

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Did you try placing the script itself in an external file and leave those step two's alone???

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #4
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>
    Last edited by pdark; 12-20-2004 at 11:56 PM.

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks liorean, will do.

  • #7
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by jamesplato; 08-17-2006 at 12:36 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •