1

On my page I have 3 tabs setup using this code along with some CSS

<ul class="subnav" id="subnav">
    <li class="active"><a href="#subover">Overview</a></li>
    <li><a href="#subnews">News</a></li>
    <li><a href="#subgallery">Gallery</a></li>
    </ul>

Inside the news Tab, I'm using .load to fill it from page arc.php and id=news.

 <div class="subcont" id="subnews">
   <script>
jQuery(document).ready(function ($) {
$('#subnews').append($('<div>').load('arc.php #news'));
});
</script> 
   </div>

On the arc.php page, I have news descriptions with links to the articles. When the article link is clicked. I want it to load into the subnews div. I'm doing that with this.

<script>  

 jQuery(document).ready(function ($) {

$("#subnews").on("click", "a", function (e) { 
    $("#subnews").load($(this).attr("href"));
    e.preventDefault();
});
 });
</script>

Now my problem...

When the news article is loaded, the url is not changed, so I'm not able to use back to get back to the news article list.

I do have code tracking hash tags, and it works when I change from Overview, News, and Gallery. I can use back to go back through the tabs.

Is there a way to add a hash tag to the url when the article is clicked in order to use tracking and be able to click back?

0

1 Answer 1

5

I'd suggest:

$("#subnews").on("click", "a", function (e) {
    var href = $(this).attr('href');
    $("#subnews").load(href);
    window.location.hash = href;
    e.preventDefault();
});

But I can't see any reason to use $(this).attr('href'), and would personally use, instead:

$("#subnews").on("click", "a", function (e) {
    var href = this.href;
    $("#subnews").load(href);
    window.location.hash = href;
    e.preventDefault();
});

References:

Sign up to request clarification or add additional context in comments.

1 Comment

this works great for adding a hash. The back button still doesn't work though. I'm guessing it's because the div content is being emptied and replaced and there is nothing to do back to. Is there a work around for this? or another way to go about this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.