如何检测浏览器强制刷新事件-跨浏览器

时间:2019-07-25 09:40:59

标签: javascript cross-browser dom-events

我想检测JavaScript上的强制刷新事件。当用户单击浏览器重新加载按钮或按 Ctrl + f5 (在Windows上)/ Cmd + r (在Mac)和刷新在移动设备上。

我尝试了 // Code inside method evaluateJSONObject() that evaluate JSON received //from http request to server that return the values for ListPreference String currentEntry = (String) listPreferenceEntrances.getEntry(); listPreferenceEntrances.setEntries(entries); listPreferenceEntrances.setEntryValues(entryValues); listPreferenceEntrances.setSummary(currentEntry); //Write the currentEntry into SharedPreferences SharedPreferences.Editor editor = sharedPref.edit(); editor.putString(getString(R.string.current_listpreference_entry_key), currentEntry); editor.commit(); 事件,但是此事件在每次加载页面时触发并导航到其他页面。

   //SharedPreference instance
    sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    setBaseUrl();

    // This show the string entry VALUE of selected ListPreference
    String entry_settings_id = sharedPref
            .getString("entry_preference", "-1");
    // This show the string entry of selected ListPreference
    String entry_settings_name = sharedPref
            .getString(
                    getString(R.string.current_listpreference_entry_key),
                    getString(R.string.current_listpreference_entry_default_value));

2 个答案:

答案 0 :(得分:1)

我能想到的最佳解决方案是Navigation Timing

window.performance属性提供了与性能相关的属性的托管区域。

if (window.performance) {
  if (performance.navigation.type === performance.navigation.TYPE_RELOAD) {
    console.info( "Reload detected" );
  } else {
    console.info( "Reload not detected");
  }
} else {
  console.info("window.performance is not supported");
}

答案 1 :(得分:0)

尝试一下:

span