按下一个HTML按钮,该按钮使用一个功能在同一个新窗口中打开URL

Pressing an HTML button that uses a function to open URLs in the same new window?

本文关键字:按钮 新窗口 同一个 窗口 URL 功能 一个 下一个 HTML      更新时间:2023-09-26

有一个HTML按钮,它调用一个JavaScript函数,该函数生成经过编辑的URL,并在新的选项卡中打开这些URL。我希望能够在一个单独的窗口中按下按钮,URL在不同的窗口中打开(但不是每个URL在一个新窗口中;它们应该在同一个窗口中打开,但按钮必须在不同的窗中)。你知道我怎样才能把按钮放在不同的窗口里吗?功能如下。

var str = 'http://www.example.com/static/page/test/test.jhtml?channelID=11&seriesID=0&episodeNumber=001';
function myFun() {  
  var n = str.indexOf("seriesID");
  var m = str.indexOf("&",n);
  var y = str.substring(n, m);
  var z = y.split('=');
  str = str.replace(y, z[0] + '=' + (parseInt(z[1]) + 1));
  var win = window.open(str, '_blank');
  console.log(str);
 if(win){
   //Browser has allowed it to be opened
 win.focus();
 } else{
  //Browser has blocked it
 alert('Please allow popups for this site');
 }
}

(按钮是用这个HTML制作的)

<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
<script src="newjs.js"></script>
<button onclick='myFun()'>Click me</button>

如果我理解正确,您想要做的是在tab-1中放置一个按钮,该按钮将打开一个新的选项卡(tab-2),然后每次单击该按钮时都可以更改tab-2源。为此,您可以执行以下操作:

function changeTabSource(newSource){
    myPopUpWindow = window.open(NewSource, "MyPopUp"); //Notice that I give it a label "MyPopUp" so that later I can reference it and change it.
}

现在,每当你想更改tab-2:的来源时,都要调用它

changeTabSource("http://google.com"); //Will change tab-2 to http://google.com
changeTabSource("http://facebook.com"); //Will change tab-2 to http://facebook.com