简体   繁体   中英

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

There is an HTML button that calls a JavaScript function which generates edited URLs and opens these URLs in new tabs. I want to be able to press the button in a separate window with the URLs opening in a different window (but not each URL in a new window; they should open in the same window but the button has to be in a different window). Do you know how I can make the button stay in a different window? The function is below.

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');
 }
}

(The button is made with this 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>

If I understood you correctly what you want to do is have a button that is placed in tab-1 that will open up a new tab ( tab-2 ) and then be able to change tab-2 source every time the button is clicked. To do this you can do the following:

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.
}

Now call it every time you want to change the source of 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM