Jump to content

Anchor


web2008

Recommended Posts

I have the "A" when I test, so I can see the target on the site.

When I test, nothing happens,  except that I can see "site2.php#targetanchor" in the browser.

As I said, link and target on the same page, is OK !

Hmm, now I see the problem!  When I "enter" again, after I see the correct url in the browser, it works!

Any idea how this can be solved?

 
 
Link to comment
Share on other sites

2 hours ago, web2008 said:

Hmm, now I see the problem!  When I "enter" again, after I see the correct url in the browser, it works!

i'm wondering if it's because it's template based, as opposed to just 2 separate php files.... i'm seeing exactly the same thing.

2 hours ago, web2008 said:

Any idea how this can be solved?

for internal links on the same page, it should work as is; for external pages (e.g linking page 1 to page 2#), then you might need to employ jQuery to scroll to the anchor...

<?php

# External Link Scroller Hook
# Written by brian!

function external_link_scroller_hook($vars)
{
	$validtemplates = array ('homepage');
	if (in_array($vars['templatefile'],$validtemplates)) {
		return <<<HTML
<script>
jQuery ( document ).ready ( function($) {
var hash= window.location.hash
if ( hash == '' || hash == '#' || hash == undefined ) return false;
      var target = $(hash);
      headerHeight = 120;
      target = target.length ? target : $('[name=' + hash.slice(1) +']');
      if (target.length) {
        $('html,body').stop().animate({
          scrollTop: target.offset().top - 125
        }, 'linear');
      }
} );
</script>
HTML;
	}
};
add_hook("ClientAreaHeaderOutput",1,"external_link_scroller_hook");

btw - I didn't write the above jQuery code, I just put it to use in the appropriate hook for your purpose (e.g so that it only gets triggered on specific pages) - possibly it might be ok to run on all pages without issue, but thought better safe than sorry. 🙂

so quickly testing this, I had a link on clientareahome.tpl....

<a href="index.php#target_anchor">Link to external page</a>

and then on homepage.tpl, I had the following anchor....

<h2 id="target_anchor">Lorem Ipsum.</h2> 

when you click on the link, it takes you to the homepage and scrolls to the specified anchor - you can have multiple anchors on the same page and separate links elsewhere to them... also it would work with <a> if you prefer to use that on your anchors, but like bear, i'm used to using other tags with these anchors.

in the above hook, there is a $validtemplates array which contains the template(s) with these anchors where this scroller script should run - just add/edit additional templates to that list when necessary.

Link to comment
Share on other sites

  • 4 weeks later...
12 hours ago, web2008 said:

What`s wrong?

is template2 a WHMCS template and that's the template filename ??

I quickly tried the following..

$validtemplates = array ('homepage','custom2');

custom2.tpl is a template used by brian4.php, so on the homepage template, I added the link as before...

<a href="brian4.php#target_anchor">Link to external page</a>

again, custom2 contains Lorem Ipsum text with the anchor used previously within it... clicking on the link in homepage, correctly takes me to the anchor on the custom page... if I disable the hook, the link just takes me to the top of the page..

what are you seeing? is it just taking you to the top of the page ??

Link to comment
Share on other sites

  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated