52

The code below displays a Google map and search results when you enter an address and hit the submit button. I've been playing with it to try and force the page to completely refresh or reload once you hit the submit button. But I can't get it to work right. It loads the results "in page," but I'd like the page to completely refresh when the results load, like when you hit the back button on your browser. Hope that makes sense.

I think the answer lies in this line of code but I don't know jquery very well. It's near the bottom of the full code below.

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

Here's the full code below. Any help would be greatly appreciated!

<?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>';

?>
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
TimMac
  • 521
  • 1
  • 4
  • 3

5 Answers5

141

You don't need jQuery to do this. Embrace the power of JavaScript.

window.location.reload()
Community
  • 1
  • 1
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • 3
    According to the syntax, would't that be: window.parent.window.location.reload()? – Diodeus - James MacFarlane Jan 30 '12 at 16:10
  • 1
    I checked the MozDevNet and found out, that the "parent" window is mostly not accessible through parent, you have to use the following attribute: https://developer.mozilla.org/en/DOM/window.opener window.opener.locaten.reload() Well window.parent already returns the reference to the parent window, because parent is of type window. – Julius F Jan 31 '12 at 08:41
  • In my case, window.location.reload() breaks my code, and window.parent.location.reload() causes the browser page to do a "strobe light" effect. I'm trying to update a Twitter feed, and when I change the subject ("screen name"), I want new tweets to replace the old ones. – B. Clay Shannon-B. Crow Raven May 15 '13 at 13:57
  • 1
    Use window.location.reload(true); because otherwise cache will be used if it is in cache already. – Codebeat Jan 20 '14 at 02:37
23

Replace that line with:

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

or:

$("#someElement").click(function() {
    window.location.reload();
});
karim79
  • 339,989
  • 67
  • 413
  • 406
  • Hey thanks guys. I'm a total noob at this stuff but when I replaced load(); with window.location.reload(); the page continually refreshes at the moment the page loads up, and doesn't stop. Can you let me know where I make the change? – TimMac May 10 '10 at 15:28
  • onsubmit="searchLocations(\''.$categories.'\'); return false;" Get rid of the "return false". – Diodeus - James MacFarlane May 10 '10 at 15:32
  • Thanks, but I'm still in an infinite refresh loop. Did I do this right: – TimMac May 10 '10 at 15:39
  • That's because every time the page loads, it refreshes again as soon as the DOM is ready. Basically, you want to bind that to the click method of some element. See the updated answer. If that element happens to be a link, `return false` at the end of the event handler. – karim79 May 10 '10 at 16:00
  • @TimMac - see my edit, you will need to include the entire code within your doc ready: `$("#someElement").click(function() { window.location.href = window.location.href; });` – karim79 May 10 '10 at 16:16
  • Thanks Karim. I tried but now the map won't load. It won't seem to load with the "load();" Here's what I tried: – TimMac May 10 '10 at 17:20
6

Or better

window.location.assign("relative or absolute address");

that tends to work best across all browsers and mobile

WiR3D
  • 1,465
  • 20
  • 23
1

Replace with:

$('#something').click(function() {
    location.reload();
});
George G
  • 7,443
  • 12
  • 45
  • 59
user1016195
  • 153
  • 3
  • 5
  • 14
1

You can refresh the events after adding new ones by applying the following code: -Release the Events -set Event Source -Re-render Events

  $('#calendar').fullCalendar('removeEvents');
                  $('#calendar').fullCalendar('addEventSource', YoureventSource);         
                  $('#calendar').fullCalendar('rerenderEvents' );

That will solve the problem