任何方式来更改标题URL而不重新加载? [英] Any way to change the header URL without reloading?
问题描述
可能存在重复:
修改URL而不重新加载页面
用新的URL更新地址栏而不用散列或重新加载页面
我的网站完全基于Ajax请求,并且网站内的简单导航仅从一个页面完成。这意味着当用户点击菜单项时,它不会加载另一个页面,而是将内容加载到HTML元素中。但是,当用户想要将页面链接到他们的朋友时,它始终是相同的,因为 http://mysite.com /mymainpage.php 将始终保持不变,而不管ajax是什么。我如何修改用户头部?例如,当他们点击游戏时,jquery / js / php / html可以在浏览器上更改当前的URL地址栏吗?以便它变成 http://mysite.com/mymainpage.php?games=true。然后用户可以从地址栏中复制网站的网址,这将导致正确的部分加载ajax。
解决方案 您可以使用散列:
(这就是Gmail的作用: https://mail.google.com/mail /#收件箱, https://mail.google.com/mail/#sent)
您可以使用
窗口读取哈希值。 location.hash / *给出#games* /
或
window.location.hash.substring(1)/ *给出游戏* /
你可以改变它使用
window.location.hash =#games
或
window.location.hash =games
(它似乎可行,但更好地使用第一个选项)
然后,每次用户点击一个链接,改变散列并像现在一样加载新页面。
当用户进入您的页面,检查URL中是否存在散列并加载该页面。
您还可以添加hashchange事件侦听器。请参阅 https://developer.mozilla.org/en-US/docs/Mozilla_event_reference/ hashchange
编辑:
有两个哈希值( http://mysite.com/mymainpage.php#games#fashion )
因此,如果用户转到 http ://mysite.com/mymainpage.php?games = true ,你应该将他重定向到 http ://mysite.com/mymainpage.php#games 。
然后您可以将它更改为 http://mysite.com/mymainpage.php#fashion 不需要重新加载。
Possible Duplicate:
Modify the URL without reloading the page
Updating address bar with new URL without hash or reloading the page
my site is completely based on Ajax requests and simple navigation within the site is done from only one single page. This means when users click the menu items it doesn't load another page rather it loads the content inside an HTML element. But when a user wants to link a page to their friend it will always be the same because http://mysite.com/mymainpage.php will always be the same regardless of what ajax pulls up. How can I modify the users header? For instance when they click on "games" can jquery/js/php/html change the current URL address bar on the browser? so that it becomes http://mysite.com/mymainpage.php?games=true. Then the user can just copy the sites url from the address bar and it will lead to the correct section for ajax to load up.
You can use hashes:
(That's what gmail does: https://mail.google.com/mail/#inbox, https://mail.google.com/mail/#sent)
You can read the hash using
window.location.hash /* gives "#games" */
or
window.location.hash.substring(1) /* gives "games" */
And you can change it using
window.location.hash="#games"
or
window.location.hash="games"
(it seems it works but better use the first option)
Then, each time user clicks a link, change the hash and load the new page like you do now.
And when the user enters to your page, check if there is a hash in the URL and load that page.
You could also add a hashchange event listener. See https://developer.mozilla.org/en-US/docs/Mozilla_event_reference/hashchange
Edit:
No, you can't have two hashes ( http://mysite.com/mymainpage.php#games#fashion )
So if an user goes to http://mysite.com/mymainpage.php?games=true, you should redirect him to http://mysite.com/mymainpage.php#games.
And then you can change it to http://mysite.com/mymainpage.php#fashion without reloading.
这篇关于任何方式来更改标题URL而不重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!