I'm currently using the jquery.address plugin to achieve deep linking.
For example, I want to load test1.html. I click on the link and it shows on the address bar /#/test1. I'd like to show only /test1 without the hashtag. I use PHP in case of a user reload the page.
Here is my code :
JS -
<script type="text/javascript">
/*<![CDATA[*/
$("document").ready(function(){
function loadURL(url) {
console.log("loadURL: " + url);
$("#area").load(url);
}
// Event handlers
$.address.init(function(event) {
console.log("init: " + $('[rel=address:' + event.value + ']').attr('href'));
}).change(function(event) {
$("#area").load($('[rel=address:' + event.value + ']').attr('href'));
console.log("change");
})
$('a').click(function(){
loadURL($(this).attr('href').replace('', /^#/));
});
});
/*]]>*/
</script>
HTML -
<a href="test1.html" rel="address:/test1">Test 1</a> <br />
<a href="test2.html" rel="address:/test2">Test 2</a> <br /> <br /> <br />
Load Area: <br />
<div id="area"></div>
Thanks in advance for your help!
loadURL($(this).attr('href').replace(/^#/, ''));
edit: Note that ^
means "beginning of string" in a regex. This means it would not match anything in the string "/#/test1"
. If you want to strip the /#/
you should be using this:
'/#/test1'.replace(/\/#\//, '');
Why not split it and take the last section?
var urlArray = $(this).attr('href').split('/');
var page = urlArray[urlArray.length - 1];
loadURL(page);