开发者

change url to another url with hashtag to use it in jquery

I use jquery tabs .. when the visitor go to : mysite.com/#user-20 I use jquery to load the user data in a hidden div .. I get data from my php file : mysite.com/user/20

开发者_运维技巧the problem is : if the user go directly to mysite.com/user/20 he will get unorganized data because there is not header of footer for that template file named (user.tpl)

I want to redirect the visitor if he print in the url ( mysite.com/user/20 ) , redirect him to ( mysite.com/#user-20) like twitter .. if you go to http://twitter.com/twitter he will redirect you to http://twitter.com/#!/twitter

I want the same ! I use codegniter framework :)


Not sure I understood it properly, but in javascript (not strictly jQuery) you can redirect the user to another page with this:

location.href = 'http://mysite.com/#user-20';


you can use this code to get current window url and then manipulate this url with your requirement(regex will be more helpfull in that case).

var url = $(location).attr('href'); 

then assign newly formed url to window in this way-

window.location.pathName = newlyFormedURL;(for ex-'http://mysite.com/#user-20')


I am not sure about how to do this in codeIgniter, but the ideal approach for the same would be to have your javascript load data in AJAX, i.e. the request mime type for the same would be application/json. Hence in your controller for the url /user/20 you should then check the request mime type and respond accordingly. If the mime type is application/json then great, otherwise you may redirect the user to the appropriate url you intended.


Thank you everybody, I really appreciate your answers :)
I did it with this code :

 var current_url = location.href ;
 if( current_url.indexOf( 'user/' ) !== -1 ) { // if not using # hashtag , so redirect him
 var user_id = current_url.substring(current_url.lastIndexOf('/')+1);
 location.href = 'http://mysite.com/#user-'+user_id ;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜