...

View Full Version : Simple VBscript acting funny



zenweezil
09-22-2005, 03:47 PM
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. :confused:

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?

nikkiH
09-22-2005, 03:55 PM
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.

zenweezil
09-22-2005, 08:10 PM
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.

zenweezil
09-22-2005, 08:22 PM
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. :)

glenngv
09-23-2005, 08:50 AM
It's very easy if you do it in javascript.


<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>

zenweezil
09-23-2005, 03:03 PM
The javascript method you offered does not have the same conflicts with my other scripts so I will employ it on my page - thanks! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum