Javascript,如何随后浏览两个网址?

时间:2016-03-25 08:59:32

标签: javascript onload chain

我有一对网友列表,foreach点击几个网址我想要的是

- open a new window (because if it's IFRAME I will get a SAMEORIGIN issue)
- browse url A 
- onload complete url A, browse urlB in the same window instance.

列表如下:

<span onclick="browseChain(urlA, urlB)">first Item</span>


<script>
      function browseChain(urlA, urlB){
         var myPopup = window.open(urlA, 'preview');
         myPopup.addEventListener('load', function(){
            console.log('url open: '+urlB);
            setTimeout("window.open('"+urlB+"', 'preview');", 2000);
         }, false);

      }    
</script>
像上面似乎Js并没有拦截&#34;加载&#34;事件...

1 个答案:

答案 0 :(得分:0)

试试这个会起作用:

JSFiddle:https://jsfiddle.net/r1pgvf3z/1/

    <span onclick="browseChain('urlA', 'urlB');">first Item</span>
<script>
function browseChain(urlA, urlB){
console.log("rohit");
         var myPopup = window.open(urlA, 'preview');
         myPopup.addEventListener('load', function(){
            console.log('url open: '+urlB);
            setTimeout("window.open(urlB, 'preview');", 2000);
         }, false);

      } 
      </script>