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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jan 2003
    Posts
    111
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Simple VBscript acting funny

    I have a simple vbscript to make text appear and disappear with the click of a link. I am using this on a FAQ page so you can quickly go down the list of questions and just open the text to the answer you want.

    Anyways - it works fine except if you are at the bottom of the page and click the link it jumps the browser to the top - not sure why it is doing that.

    Here is the script:
    <script LANGUAGE="VBScript">
    <!--
    sub show (id)
    if id.style.display = "" then
    id.style.display = "none"
    else
    id.style.display = ""
    end if
    end sub

    -->
    </script>

    Here is a link:
    <a href="#" onclick="show(q1)">Question here</a>

    Here is the span this link would open:
    <span id="q1" style="display: none">Answer here</span>

    LIke I said it works fine except it wants to kick the browser up the top of page if you are at the bottom whne you click.

    Any thoughts?

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    You do realize vbscript on the client is only supported by IE, so those of us who won't or can't use it are screwed, right?

    Anyway:
    <a href="#" onclick="show(q1); return false;">Question here</a>

    It jumps to the top because it follows the link. Put the return false in there to stop that.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    Regular Coder
    Join Date
    Jan 2003
    Posts
    111
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Yes, I knew that - this is acutally an in-house FAQ on a Extranet site and the company uses IE exclusively so I figured why not.

    Anyways, I tried adding return false to the onclick attribute, but it stopped the function from working and it was still following the link.

    So, I think now it may be conflicting with other scripts on the page so I may have to come at this another way.

  • #4
    Regular Coder
    Join Date
    Jan 2003
    Posts
    111
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Resolved

    In case your curious - what I decided to do was just go with it and put anchors at each question so when it follows the link it doesn't go anywhere - not an ideal coding solution but it gives the desired results - so I'm happy.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    It's very easy if you do it in javascript.
    Code:
    <script type="text/javascript">
    function show(id){
      var ans = document.getElementById(id);
      ans.style.display = (ans.style.display=="none") ? "inline":"none";
      return false;
    }
    </script>
    ...
    <a href="#" onclick="return show('q1');">Question here</a>
    <span id="q1" style="display: none">Answer here</span>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    Regular Coder
    Join Date
    Jan 2003
    Posts
    111
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thanks

    The javascript method you offered does not have the same conflicts with my other scripts so I will employ it on my page - thanks!


  •  

    Posting Permissions

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