how to use window.location.hash?

I don't really know the concept of hash but based on what I understand, It prevents a page from redirecting to other page when the user hits the F5 key.

I have this jquery function:

$('#f1').click(function()
{
    alert(window.location.hash);
    $("#body").load("sample.php");

});

HTML:

<a href = "#!sample" id = "f1">FUNC 1</a>
<div id = "body">
<!-- OUTPUT HERE -->
</div>

It successfully outputting the content of my sample.php, but when I hit the refresh button or F5 in the keyboard it redirected back to the parent page..

I want that when I hit refresh the content of sample.php will remain in the body.

Upvotes: 1

Views: 2151

Answers (2)

Tiquelou
Tiquelou

Reputation: 459

One way in which you can make sure that the content loads on refresh is by calling the .load() function on document load, i.e.

$(function() {
    $("#body").load("sample.php");
)};

Unless there is some very strong reason why you are using a click to reload the content in the #body div

Upvotes: 0

rekire
rekire

Reputation: 47975

No that is wrong. The hash is just a anchor that allows the user to jump into a static position into a document.

For much applications the hash is used for saving states which is not anymore needed there are some technics to change the URL without reloading for the same domain. See also the MDN site.

The page is just not reloaded if you change the hash, but if the user presses F5 the page will be reloaded.

Upvotes: 3

Related Questions