Domanda

I am trying to load an anchor tag on an external page. I am using this method:

<a href="page.html#div>

to link to another page's specific div.

<div name="div">
stuff
</>

The problem so far had been that the link only linked me to the external page instead of to the specific div on that page. I have just discovered that the issue lies with my inclusion of jquery. When I press the link, the correct div appears for a split second and then the page quickly changes back to the top of the page.

Is there a way to disable jQuery's auto-scroll on the external page through a method like this:

 $(document).ready(function(e) {
 e.preventDefault();  
 });

I tried including this on my links:

onclick="return false;

But it means they do not work.

È stato utile?

Soluzione

The anchor works with the name attribute in <a> tags, not with a <div>.

The link:

<a href="page.html#here>
to link to another page's specific div.
</a>

The target destination:

<a name="here">
stuff
</a>

If you are trying to mess with this behavior using jQuery (e.g. prevent it via javascript in the target page), then there is a problem because the browser loads the page FIRST and THEN runs the javascript in the page so unless the default condition of your destination page is hidden, the content may show one way before the jQuery can modify it. Unless you can initially hide the target page with static CSS style rules (not with javascript), there isn't anything you can do about this because the page may show before your javascript runs.

If you can put javascript in the first page, you could remove the #here from the links so when they are clicked, they don't have that extra target in them.

If you can modify the HTML of the second page, you can remove the name targets so the #here doesn't do anything.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top