Javascript PHP模板系统

What I currently have:

  • index.php: Mainpage, showing topbar, navigation & placeholder for content

  • navigation: uses javascript to load a php-file into the content-section from index.php if a link in the navigation is clicked; important: NO refresh of index.php

Is working fine right now. Also the url in the address bar is not being changed right now when clicking a link.

What should be added:

If a link in the navigation is clicked, javascript should still load a php-file but also should change the url in the address bar without refreshing the side.

Example:

  • url: "www.example.com
  • In navigation link "work" is clicked.
  • work.php is loaded into the content-section of index.php
  • url now: "www.example.com/work

Additionally if the user types www.example.com/work in the address bar index.php should load work.php without a link being clicked.

A good example to understand my issue with the url in the address bar is facebook. Hope I could clarify my problem so somebody is able to help me ;-)

You can only change the hashtag of a URL without refreshing the page (making another request). So when a navigation link is clicked you could change "www.example.com" to "www.example.com/#work" but you can't change it to "www.example.com/work" without reloading the page.

A hashtag can get set with javascript using window.location.hash

window.location.hash = "work";

If you are looking to avoid hashtags, you can use the HTML5 history API. This allows you to have the / in your pages using the history.pushState() and history.replaceState() method. There is also this plugin that you may find useful.