简体   繁体   English

创建指向刷新页面的ID的链接

[英]Creating a link to an id that refreshes the page

I am working on a project that uses javascript to make tabs. 我正在使用javascript制作标签的项目。 It has 3 tabs. 它具有3个标签。

Tab three has an id of tab3, what I need is a refresh button that will be on tab3 that will refresh the page. 标签3的ID为tab3,我需要的是将在tab3上的刷新按钮,以刷新页面。 Problem I'm having is if you click on tab3 it will display tab3 data, but it does not change the url. 我遇到的问题是,如果您单击tab3,它将显示tab3数据,但不会更改url。 So the refresh button isn't working. 因此刷新按钮不起作用。

I tried an onclick javascript that reloads the page using location.reload() which reloads the page but not to the tab3. 我尝试了一个onclick javascript,它使用location.reload()重新加载页面,但重新加载了页面但未加载到tab3。 So I put that on a link, 所以我把它放在一个链接上,

<a href="<?php echo $_SERVER['REQUEST_URI']; ?>#tab3" onclick="reloadPage();" >Refresh</a> 

This will link to the correct location, the URL then has #tab3 at the end, then the Javascript runs and it reloads back to the url without the #tab3. 这将链接到正确的位置,URL末尾带有#tab3,然后Javascript运行,并且重新加载回不含#tab3的URL。

So I then tried this that I saw somewhere. 因此,我尝试了在某处看到的这个。

<a href="javascript:;" onclick="window.location = '<?php echo $_SERVER['REQUEST_URI']; ?>#tab3;'">Go</a>

Which links, but doesn't reload. 哪个链接,但不重新加载。

What is the best way to do this? 做这个的最好方式是什么?

尝试这个-

<a href="#tab3" onclick="location.reload();" >Refresh</a> 

In head put, 在头顶上

<script type='text/javascript'>
function goto(link) {
window.location = link
}
</script>

and do this to your button 并按此按钮

<a onclick=goto("<?php echo $_SERVER['REQUEST_URI'] . '#tab3'; ?>"); >GO </a>

if you want to refresh the page and aslo goto same place this code will work 如果您想刷新页面和转到相同的位置,则此代码将起作用

<form method=post action="">
<input type=hidden name=value value="#tab3" />
<input type=submit value=GO />
</form>
<?php
if (isset($_POST['value']))
{
header('Location: http://'. $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI] . $_POST["value"]);
}

?>

Using window.location is the solution, however if the current location is already the same as the new url that is assigned, then browser will not re-load the page. 使用window.location是解决方案,但是,如果当前位置已经与分配的新URL相同,则浏览器将不会重新加载页面。 The only trick would be to make the new url look "new". 唯一的技巧是使新的网址看起来像“新的”。 This can be done by adding a random identifier as querystring parameter. 这可以通过添加随机标识符作为querystring参数来完成。
In the form: 形式:

http://hostname?<dynamic/random>#tab3

Example: 例:

<a href="javascript:;" onclick="window.location = '<?php echo $_SERVER['REQUEST_URI']; ?>' + Date.now() + '#tab3;'">Go</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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