Alejandro DC Alejandro DC - 20 days ago 8
HTML Question

Call a javascript function throught URL

I have a website with tabs, where each tab is in fact in the same table with an atribute that show it or not. Then a javascript function would change the attribute when you press on the tab.

The function is something like this:

function showHide(ID) {
switch (ID) {
case 'main':
document.getElementById('main').style.display = 'block';
document.getElementById('abstracts').style.display = 'none';
break;
case 'abstracts':
document.getElementById('main').style.display = 'none';
document.getElementById('abstracts').style.display = 'block';
break;
}
return true;
}


Then, the title of the tab is something like

<a href="" onclick="showHide('main');return false;">Main</a>


and the tab is

<tr id="main"> ... </tr>


The thing is that I would like to be able to have a URL for a tab, for example http://mypage.com#main or something like that, so when I enter to that URL, the tab main is focussed.

Is that possible?

Answer

At the bottom of your page or window.onload, read the hash and call your method.

(function(){
    var hash = window.location.hash.substr(1);
    showHide(hash);
}());

and if you want the url to change, you probably want to remove the return false.

Comments