I have a page, which has 3 placeholders with text. These are all hidden on page_load, and jQuery sets them to visible depending on whether there is a hashtag or not.
I want to use inline linking using hashtags like <a href="#references"></a>
. Depending on the hashtag, I want to show/hide other content.
It would be easy to get to work using querystrings, but unfortunately, that will give very bad search engine optimization in my case.
I got a solution working, but there is one major problem. When the user clicks my menu in the top, which appends the hashtag, it goes to the link, but my jQuery is first run on the second click. The following happens:
- We are on URL http://www.domain.com
- The user clicks on my menu and the url is http://www.domain.com#info , but nothing changed in the markup
- The user clicks the menu the second time, url is same, but now the markup changes
My jQuery script:
<script>
$(document).ready(function () {
updateview();
});
function updateview() {
if (document.location.hash.search('calculator') !== -1) {
$("#ContentContainer").hide();
$("#VideoPnl").hide();
$("#CalculatorPnl").show();
}
else if (document.location.hash.search('video') !== -1) {
$("#CalculatorPnl").hide();
$("#VideoPnl").show();
$("#ContentContainer").hide();
} else {
$("#ContentContainer").show();
$("#CalculatorPnl").hide();
$("#VideoPnl").hide();
}
}
My menu
Every menu point at my menu have the following JavaScript added, which I hoped would change the screen when clicking, but this one first works the second time:
menu.Attributes.Add("onclick", "javascript:updateview()");
My markup in ASP.NET:
<asp:Panel runat="server" ID="ContentContainer" ClientIDMode="Static">
<asp:Literal runat="server" ID="ContentPlaceholder"></asp:Literal>
</asp:Panel>
<asp:Panel ID="CalculatorPnl" runat="server" ClientIDMode="Static">
<asp:Literal runat="server" ID="CalculatorHeadlineLtl"></asp:Literal>
<asp:Literal runat="server" ID="CalculatorPlaceholder"></asp:Literal>
</asp:Panel>
<asp:Panel ID="VideoPnl" runat="server" ClientIDMode="Static">
<asp:Literal runat="server" ID="VideoHeadlineLtl"></asp:Literal>
<asp:Literal runat="server" ID="VideoPlaceholder"></asp:Literal>
</asp:Panel>