javascript:history.go(-1)无法使用IE和Chrome

时间:2018-05-25 09:59:44

标签: javascript

我正在尝试使用下面的代码来遍历上一页,它在IE和Chrome上无效,但在Edge上运行正常。

package com.test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Test {

    public static void generateData(final int[] arr) {
        final Random aRandom = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = aRandom.nextInt(10);
        }
    }

    public static void calculateAllOddOccurrence(final int[] arr) {
        final Map<Integer, Integer> mymap  = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            if (mymap.containsKey(arr[i])) {
                mymap.put(arr[i], mymap.get(arr[i]) + 1);
            } else {
                mymap.put(arr[i], 1);
            }
        }
        for (final Map.Entry<Integer, Integer> entry : mymap.entrySet()) {
            if (entry.getValue() % 2 != 0) {
                System.out.println(entry.getKey() + "=" + entry.getValue());
            }

        }
    }

    public static void calculateAllOddOccurrenceStream( int[] arr) {
        Arrays.stream(arr).boxed().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().parallelStream().filter(e -> e.getValue() % 2 != 0).forEach(entry -> System.out.println(entry.getKey()+"="+ entry.getValue()));
    }

    public static void calculateAllOddOccurrenceStream(List<Integer> list) {
        list.parallelStream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().parallelStream().filter(e -> e.getValue() % 2 != 0).forEach(entry -> System.out.println(entry.getKey()+"="+ entry.getValue()));
    }

    public static void main(final String... doYourBest) {

        final int[] arr = new int[200000000];

        generateData(arr);
        long starttime = System.currentTimeMillis();
        calculateAllOddOccurrence(arr);
        System.out.println("Total time with simple map=" + (System.currentTimeMillis() - starttime));

        List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());
        starttime = System.currentTimeMillis();
        calculateAllOddOccurrenceStream(list);
        System.out.println("Total time stream - with a readymade list, which might be the case for most apps as arraylist is more easier to work with =" + (System.currentTimeMillis() - starttime));

        starttime = System.currentTimeMillis();
        calculateAllOddOccurrenceStream(arr);
        System.out.println("Total time Stream with array=" + (System.currentTimeMillis() - starttime));

    }}

还按照我读过的一些帖子尝试了这个

0=19999427
2=20001707
4=20002331
5=20001585
7=20001859
8=19993989
Total time with simple map=2813
4=20002331
0=19999427
2=20001707
7=20001859
8=19993989
5=20001585
Total time stream - with a readymade list, which might be the case for most apps as arraylist is more easier to work with = 3328
8=19993989
7=20001859
0=19999427
4=20002331
2=20001707
5=20001585
Total time Stream with array=6115

仍然是相同的结果。 有什么想法吗?

2 个答案:

答案 0 :(得分:0)

history.go与Chrome和IE support it同样没问题。这段代码确实有效:

document.getElementById("mylink").addEventListener("click", function() {
    alert("Hello world!")
    // Or alternatively:
    //history.go(-1);
});
<a href="javascript:history.go(-1)">Navigate 1 page backwards</a>
<br /><br />

<a href="javascript:alert('Hello World!')">Announce "Hello World"</a>
<br /><br />

<a href="javascript:alert('Hello World!')">
  <label>This is a label wrapped in an a tag which is not recommended, and will not work on certain browsers such as IE (and for example in this specific case does not show the pointer cursor in Chrome)</label>
</a>
<br /><br />

<label>
<a href="javascript:void(0)" id="mylink">
    This is correct practice. No inline js, and the a tag is in the label tag and not the other way around (which only matters if you want to work with IE as other modern browsers allow this).
</a>
</label>

以上适用于我使用Chrome 66(最新版)。也许尝试使用inspect元素查看ASP在浏览器中呈现的内容,并检查某些代码是否可能无效。所有上述解决方案都应该有效。

此外,使用内联js被认为是不好的做法。 了解详情:

答案 1 :(得分:-1)

尝试:

window.history.back()

window.history.forward()

参考“穿越历史”:https://developer.mozilla.org/en-US/docs/Web/API/History_API