I'm new to JavaScript and hope somebody can help me with the following.
I'd like to create some tabs with one active tab. Now the problem is I don't want to load all tabs at once, but only load the content of the active tab, the first tab by default and the content of the other tabs stored in a database.
I thought this would be possible with JavaScript and/or Ajax (however I'm new to this matter) but I only can find tutorials where all content of all tabs is loaded at once like the following:
...then to demonstrate the example create a PHP file, in the same folder as the file containing the above, with the following:
PHP Code:
switch($_GET['tabNum']) {
case 1: echo "this is the content for the <b>first</b> tab"; break;
case 2: echo "this is the content for the <b>second</b> tab"; break;
case 3: echo "this is the content for the <b>third</b> tab"; break;
case 4: echo "this is the content for the <b>fourth</b> tab"; break;
case 5: echo "this is the content for the <b>fifth</b> tab"; break;
}
Depends on your level. I have Douglas Crockford's Javascript: The Complete Guide, but it's not for beginners; it's hundreds of pages and covers some advanced topics in-depth.
You really don't need a whole book on Ajax. I made that mistake; whilst there is more to Ajax than just loading and retrieving stuff with XMLHTTP, you won't use it much. Any good Javascript book will teach you Ajax, anyway.
To modify the PHP script to return DB content, you just need to replace my "echo" statements with mysql functions. I assume you know enough PHP to do this.
There is just one problem though. The tabs I'm about to use are quite large so they take a lot of the webpage.
Is there a possibility the URL structure changes when clicking another tab? The reason is people probably would like to point to a particular tab. When using the current code they always have to point to the default tab.
And remove class='on' from the first tab's <li> tag.
Then, you can stipulate in the URL which tab should start as on via test.php?tabNum=3. If tabNum is not passed in the URL, or it's not numeric, the first tab will be on by default. Make sense?
I have changed the code and removed the class='on' as you suggested.
When surfing the tabs the URL structure don't change. So when I'm clicking on the third tab, the URL is still domain.com/test.php instead of domain.com/test.php?tabNum=3
However when I enter domain.com/test.php?tabNum=3 immediately in the browser the third tab opens, but it is unlikely visitors will do this manually.
Yes, that's what I intended. I didn't realise you wanted the URL to change on each click. In that case, there's no reason to use Ajax - might as well just use conventional loading.
Alas, you can't artificially change a URL - you have to actually go to the page. The only way of changing the URL in the address bar without loading a new page is to append an anchor (#).
You could change the code from my last post to read an anchor, instead of a variable named ?tabNum.
Replace the 3 lines I gave you in my last post with:
I guess so but you'd have to change my code to detect /pictures or whatever, then interpret that as a tab number.
But this would cause you problems because you want the URL to change on each click, without loading a new page. The only way to do this is with anchors (#). If you change the URL from /pictures to, say, /videos, you will change page and it will reload.
Why not use a system of named anchors instead, so not #1, #2 etc but #pictures, #videos etc.
Bookmarks