如何在jQuery中强制刷新页面或重新加载页面?

52
下面的代码在你输入地址并点击提交按钮时显示Google地图和搜索结果。我一直在尝试强制页面在点击提交按钮后完全刷新或重新加载,但是我无法正确地使其工作。它会在页面中加载结果,但是我希望页面在加载结果时完全刷新,就像当您在浏览器上点击后退按钮时一样。希望这讲得清楚。我认为答案在下面完整代码的这行代码中,但我不太了解jQuery。
<script type="text/javascript">
(function($) { 
    $(document).ready(function() {
        load();';

以下是完整的代码。非常感谢您的帮助!

<?php
/*
SimpleMap Plugin
display-map.php: Displays the Google Map and search results
*/
$to_display = '';

if ($options['display_search'] == 'show') {
$to_display .= '
<div id="map_search" style="width: '.$options['map_width'].';">
    <a name="map_top"></a>
    <form onsubmit="searchLocations(\''.$categories.'\'); return false;"         name="searchForm" id="searchForm"     action="http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'">
        <input type="text" id="addressInput" name="addressInput" class="address"     />&nbsp;
        <select name="radiusSelect" id="radiusSelect">';

            $default_radius = $options['default_radius'];
            unset($selected_radius);
            $selected_radius[$default_radius] = ' selected="selected"';

            foreach ($search_radii as $value) {
                    $r = (int)$value;
                    $to_display .= '<option valu         e="'.$value.'"'.$selected_radius[$r].'>'.$value.' '.$options['units']."</option>\n";
            }

$to_display .= '    
        </select>&nbsp;
        <input type="submit" value="'.__('Search', 'SimpleMap').'"     id="addressSubmit" class="submit" />
        <p>'.__('Please enter an address or search term in the box above.',     'SimpleMap').'</p>
    </form>
</div>';
}
if ($options['powered_by'] == 'show') {
    $to_display .= '<div id="powered_by_simplemap">'.sprintf(__('Powered by %s     SimpleMap', 'SimpleMap'),'<a href="http://simplemap-plugin.com/"     target="_blank">').'</a></div>';
}

$to_display .= '
<div id="map" style="width: '.$options['map_width'].'; height:     '.$options['map_height'].';"></div>

<div id="results" style="width: '.$options['map_width'].';"></div>

<script type="text/javascript">
(function($) { 
    $(document).ready(function() {
        load();';    

        if ($options['autoload'] == 'some') {
            $to_display .= 'var autoLatLng = new GLatLng(default_lat,     default_lng);
            searchLocationsNear(autoLatLng, autoLatLng.lat() + ", " +     autoLatLng.lng(), "auto", "'.$options['lock_default_location'].'", "'.$categories.'");';
        }

        else if ($options['autoload'] == 'all') {
            $to_display .= 'var autoLatLng = new GLatLng(default_lat,     default_lng);
            searchLocationsNear(autoLatLng, autoLatLng.lat() + ", " +     autoLatLng.lng(), "auto_all", "'.$options['lock_default_location'].'",     "'.$categories.'");';
        }

$to_display .= '
    });
})(jQuery);
</script>';

?>
5个回答

141

你不需要 jQuery 来实现这个功能。拥抱 JavaScript 的强大之处。

window.location.reload()

3
根据语法,这不应该是:window.parent.window.location.reload() 吗? - Diodeus - James MacFarlane
1
我查看了MozDevNet并发现,“parent”窗口大多数情况下无法通过parent访问,您必须使用以下属性:https://developer.mozilla.org/en/DOM/window.opener window.opener.locaten.reload() 因为parent是window类型,所以window.parent已经返回对父窗口的引用。 - Julius F
在我的情况下,window.location.reload() 会破坏我的代码,而 window.parent.location.reload() 则会导致浏览器页面出现“闪光灯”效果。我正在尝试更新 Twitter 动态,当我更改主题(“屏幕名称”)时,我希望新的推文替换旧的推文。 - B. Clay Shannon-B. Crow Raven
1
请使用window.location.reload(true);,否则如果已经在缓存中,则会使用缓存。 - Codebeat

23

将那行代码替换为:

$("#someElement").click(function() {
    window.location.href = window.location.href;
});

或者:

$("#someElement").click(function() {
    window.location.reload();
});

嘿,谢谢大家。我对这个东西完全是个新手,但当我用window.location.reload()替换了load();时,页面在加载时不断刷新,并且无法停止。你们能告诉我在哪里做出更改吗? - TimMac
onsubmit="searchLocations(''.$categories.''); return false;" 去掉 "return false"。 - Diodeus - James MacFarlane
谢谢,但我仍然处于无限刷新循环中。我这样做对了吗:<script type="text/javascript"> (function($) { $(document).ready(function() { window.location.reload();'; - TimMac
这是因为每次页面加载时,DOM准备就绪后会再次刷新。基本上,您想将其绑定到某个元素的点击方法上。请参见更新的答案。如果该元素恰好是链接,则在事件处理程序的末尾返回false。 - karim79
@TimMac - 请看我的编辑,你需要在doc ready中包含整个代码:$("#someElement").click(function() { window.location.href = window.location.href; }); - karim79
谢谢Karim。我尝试了,但现在地图无法加载。它似乎无法通过“load();”加载。这是我尝试的:<script type="text/javascript"> (function($) { $(document).ready(function() { $("#addressSubmit").click(function() { window.location.href = window.location.href; }); - TimMac

6
或者更好。
window.location.assign("relative or absolute address");

在所有浏览器和移动设备上都表现最佳的是以下技术:


1
你可以通过应用以下代码来在添加新事件后刷新事件: -释放事件 -设置事件源 -重新渲染事件
  $('#calendar').fullCalendar('removeEvents');
                  $('#calendar').fullCalendar('addEventSource', YoureventSource);         
                  $('#calendar').fullCalendar('rerenderEvents' );

那将解决问题。

1

Replace with:

$('#something').click(function() {
    location.reload();
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接