Web Analytics Made Easy -
StatCounter Newbie in need of urgent help! - CodingForum

Announcement

Collapse
No announcement yet.

Newbie in need of urgent help!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newbie in need of urgent help!

    I'm a JS newbie in dire need of advice for my something I'm working on for my senior project. I need to create a button that links to another website. That's usually a simple matter of filling in the "onclick" field with the website address, but the way I need to do it has a twist. Instead of doing it in straight HTML, I need JavaScript. Here is, in essence, what I'm trying to pull off:

    First, I have an array in which each member of that array is a website, as such:

    var links = new array(3);
    links[0]="http://www.website1.com";
    links[2]="http://www.website2.com";
    links[3]="http://www.website3.com";

    Next, I have three buttons in my HTML in the body that I need to link to each website, respectively. So, what do I do with the "onclick" field so that it calls up the JavaScript above (I presume through a function I must create) and goes to one of the websites in the array?
    The James Website:
    www.angelfire.com/realm/angelicknight

  • #2
    onclick="location.href=links[1]"

    or

    onclick="load(1);"


    function load(I)
    {
    location.href=links[I]"
    }

    Comment


    • #3
      Thanks, that's what I'm looking for! I'm still having a little trouble applying it though, I'm doing something wrong and my button isn't working right yet. When I click the button in my browser, I get an error because it tries to look for the website as if it were a page on my website on which this code exists, instead of treating it like an entirely seperate website...I imagine I must be overlooking something obvious.

      <script language="JavaScript">
      var members = new array(3);

      members[0] = "http:www.bryantcivitan.org";
      members[1] = "http:www.angelfire.com/realm/angelicknight";
      members[2] = "http:www.downtownlr.com";

      function load(i)
      {
      location.href="members[i]"
      }
      </script>

      -----------------------------------------------------------------------------------

      <form> <input type="button" name="GoCivitan" value="Civitan"
      onclick="load(0);"></form>

      -----------------------------------------------------------------------------------

      You can also see the actual page this code is in at http://www.angelfire.com/realm/angel...ts/script.html
      Last edited by AngelicKnight; Feb 22, 2004, 04:15 PM.
      The James Website:
      www.angelfire.com/realm/angelicknight

      Comment


      • #4
        javascript is case sensitive.

        it is new Array not new array

        and when you assign a variable value to another variable it is not inclosed in ""


        <script language="JavaScript">
        var members = new Array(3);

        members[0] = "http://www.bryantcivitan.org";
        members[1] = "http:www.angelfire.com/realm/angelicknight";
        members[2] = "http:www.downtownlr.com";

        function load(i)
        {
        location.href=members[i]
        }
        </script>

        <form> <input type="button" name="GoCivitan" value="Civitan"
        onclick="load(0);"></form>

        Comment


        • #5
          That did it, thanks everybody. The buttons are working just as I wanted.
          The James Website:
          www.angelfire.com/realm/angelicknight

          Comment


          • #6
            I'm suprised that http:www.angelfire.com/realm/angelicknight works.
            I think you need the // in there
            http://www.angelfire.com/realm/angelicknight
            Shawn

            Comment


            • #7
              You're right, it won't. I included that in my latest revision of my code though.
              The James Website:
              www.angelfire.com/realm/angelicknight

              Comment

              Working...
              X