[英]How can I reload page two from button in page one?
I've button in page one and I wanna to refresh page two when I click on button in page one ?? 我在第一页按钮,当我点击第一页的按钮时,我想刷新第二页?
I've been using this code but not working : 我一直在使用这段代码,但没有工作:
var url_home = "{{ url('/') }}";
$(document).ready(function()
{
$("#refreshDIV").click(function()
{
$("#refreshDIV").reload(url_home);
});
});
You can use local storage to accomplish this in the browser as long as both pages are open at the same time. 只要两个页面同时打开,您就可以使用本地存储在浏览器中完成此操作。
In your page ONE , you can set local storage like below: 在您的第一页 ,可以设置本地存储象下面这样:
$("#refreshDIV").click(function()
{
localStorage.setItem('refreshOtherPage', '0');
localStorage.setItem('refreshOtherPage', '1');
});
Then in page TWO , you can add a listener script to automatically reload the page: 然后,在第二页,你可以添加一个监听器脚本来自动重新加载页面:
//Fired from page ONE tab
$(window).on('storage', function (e)
{
var storageEvent = e.originalEvent;
if ((storageEvent.key == 'refreshOtherPage')
&& (storageEvent.oldValue == '0')
&& (storageEvent.newValue == '1'))
{
// DO WHATEVER YOU WANTED TO DO WHEN THAT BUTTON WAS CLICKED HERE
}
});
It seems you mix javascript and jquery. 看来你混合了javascript和jquery。 The
reload()
function does not exist in jquery. jquery中不存在
reload()
函数。 However, you are close to answer your question. 但是,您即将回答您的问题。 But you'll have to put your 'second page' into a div#id.
但是你必须将你的'第二页'放入div#id。
<div id="page1"><button id="reload" ...></div>
<div id="page2">the second page, no page headers</div>
And your jquery 而你的jquery
var url_home = "{{ url('/') }}";
$(document).ready(function()
{
$("button#reload").click(function()
{
//font awesome spinner while loading, temporarily replaces #page2
$("#page2").html('<div class="fas fa-sync fa-spin fa-2x fa-fw"></span>');
$("#page2").load(url_home);
});
});
You could make the button dynamic by replacing it with a boostrap <a>
button and catch the url let url_home = $(this).attr('href');
您可以通过用boostrap
<a>
按钮替换它来使按钮动态化并捕获url let url_home = $(this).attr('href');
, don't forget to preventDefault()
and trigger on $("a#reload">
if you go for this option. ,不要忘记使用
preventDefault()
并触发$("a#reload">
如果你选择这个选项。
If I understand you correctly, you can do that by naming your link's target attribute when you first launch the new tab. 如果我理解正确,您可以在首次启动新选项卡时命名链接的目标属性。 Then every time you click that link used to launch the tab, it will reload the page in that tab for you.
然后,每次单击用于启动选项卡的链接时,它都会为您重新加载该选项卡中的页面。
In page A you add a link to page B like this: 在页面A中,您可以像这样添加指向页面B的链接:
<a href="/page-b" target="page_b">Foo</a>
Now click that link and it will open page B in new tab. 现在单击该链接,它将在新选项卡中打开页面B.
Click again and it will reload page B. 再次单击它将重新加载页面B.
This will not work when someone directly goes to /page-b
though. 当有人直接访问
/page-b
时,这不起作用。
There is no need to write any Javascript or something on server side. 无需在服务器端编写任何Javascript或其他内容。 However, if you must create a button dynamically you may adapt this approach to suite your needs.
但是,如果必须动态创建按钮,则可以调整此方法以满足您的需求。 Hope this helps!
希望这可以帮助!
If you meant reloading part of the same webpage, that is easily achievable through AJAX. 如果您想重新加载同一网页的一部分,那么可以通过AJAX轻松实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.