简体   繁体   中英

javascript refresh current page with fragment

How do I refresh the current page with a fragment?

If a page url is like: localhost/test#1 do <a href="http://localhost/test#2"> how do I refresh the current page?

location.reload(true)

window.location.reload()

These 2 methods will all reload the page still to localhost/test#1 . Thanks.

window.location.hash ?

setTimeout((
    function(){
        window.location.hash = '#2';
    }
), 2000);

// so: <a href="http://localhost/test#2" onclick="window.location.hash='#2';window.location.reload();">aaa</a>

You're going to want to take a closer look at the hashchange event. Fortunately there is a nice jQuery plugin called BBQ.

http://benalman.com/projects/jquery-bbq-plugin/

# urls works on client only. part after # does not send to server

You can use Url Rewriting

In your .htaccess file add those lines:

RewriteEngine On
RewriteRule ^([0-9a-zA-Z_]*)$ index.php?p=$1 [L]

In your page test/index.php you can use the link(without the #):

<a href="2">Page 2</a>

It will store it in $_GET['p'] (in the example the target is php)

Find the rewriting rule that answer your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM