Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 17th, 2005, 04:08 PM
Registered User
 
Join Date: Jan 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default onClick and go to anchor #something

I seem to have a problem: I have a button that runs a js function, and I want it to work so that when you press the button, it runs the function (collapse a table) and scrolls to an anchor #something.

<a href="#something" onclick="return toggle_collapse('1200')"><img src="images/button.gif" alt="button" border="0" /></a>

But this doesn't work.. when i click it, it only collapses the table but doesnt move to the <div id="something> labeled location.

Does this have anything to do with the code of the func itself, or is there a workaround?

Reply With Quote
  #2 (permalink)  
Old January 17th, 2005, 04:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: High Wycombe, UK, United Kingdom.
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

possible, can you post your Javascript function ?
Reply With Quote
  #3 (permalink)  
Old January 17th, 2005, 04:44 PM
Registered User
 
Join Date: Jan 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

its a vbulletin function.. I posted it on their website, noone responded.

this is the function
Code:
// #############################################################################
// function to toggle the collapse state of an object, and save to a cookie
function toggle_collapse(objid)
{
    if (!is_regexp)
    {
        return false;
    }

    obj = fetch_object("collapseobj_" + objid);    
    img = fetch_object("collapseimg_" + objid);
    cel = fetch_object("collapsecel_" + objid);

    if (!obj)
    {
        // nothing to collapse!
        if (img)
        {
            // hide the clicky image if there is one
            img.style.display = "none";
        }
        return false;
    }

    if (obj.style.display == "none")
    {
        obj.style.display = "";
        save_collapsed(objid, false);
        if (img)
        {
            img_re = new RegExp("_collapsed\\.gif$");
            img.src = img.src.replace(img_re, '.gif');
        }
        if (cel)
        {
            cel_re = new RegExp("^(thead|tcat)(_collapsed)$");
            cel.className = cel.className.replace(cel_re, '$1');
        }
    }
    else
    {
        obj.style.display = "none";
        save_collapsed(objid, true);
        if (img)
        {
            img_re = new RegExp("\\.gif$");
            img.src = img.src.replace(img_re, '_collapsed.gif');
        }
        if (cel)
        {
            cel_re = new RegExp("^(thead|tcat)$");
            cel.className = cel.className.replace(cel_re, '$1_collapsed');
        }
    }
    return false;
}
Reply With Quote
  #4 (permalink)  
Old January 17th, 2005, 05:27 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: High Wycombe, UK, United Kingdom.
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

all you need to do is the work out what your anchor is for each element and then put that value into a variable and then do something like

var anchorRef="something"; // calculated value

document.location=document.location + "#" + anchorRef;
Reply With Quote
  #5 (permalink)  
Old January 17th, 2005, 05:42 PM
Registered User
 
Join Date: Jan 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't really understand.. where do i put this. And also.. the location of the anchor can change on different pages, since vbulletin uses mysql and php. I just need to know if there is a way to go to that anchor without using href="#something".

Like
Code:
<a href="#" onClick="return toggle_collapse('15150'); anchor(#something)">
or something of that sort

Reply With Quote
  #6 (permalink)  
Old January 17th, 2005, 07:26 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: High Wycombe, UK, United Kingdom.
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

something like :

<a href="#" onClick="return toggle_collapse('15150'); document.location=document.location+'#something';" >

may work.
Reply With Quote
  #7 (permalink)  
Old January 18th, 2005, 06:16 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can also set the location.hash property directly to the anchor's name.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #8 (permalink)  
Old November 28th, 2006, 06:45 AM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Don't listen to their bullshi t. Just remove "return false;" in the javascript code would do the job.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Anchor Tag kapilmirchi ASP.NET 1.0 and 1.1 Basics 2 November 18th, 2008 05:57 AM
anchor and get variables. SignalFade Beginning PHP 4 January 19th, 2006 08:56 PM
Javascript in Anchor Tag jamara JSP Basics 1 May 30th, 2005 01:00 AM
Anchor Tags and the onClick event Blaise Javascript 4 June 16th, 2003 04:58 AM



All times are GMT -4. The time now is 07:19 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.