Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am making a multiple url opener too in HTML and Javascript. I am getting some problems that HTTP links are opening but HTTPS links are not. Can anyone help me with that? Here is the code.

function open_all() {
  var urls = document.getElementById("list_urls").value;
  var urls = urls.split('
');
  var totalno = urls.length;
  var s;
  for (var i = 0; i < totalno; i++) {
    s = urls[i];
    if (s) {
      if (s.substr(0, 7) != 'http://') {
        s = 'http://' + s;
      }
      window.open(s);
    }
  }
  return false;
}
<form method="post" action="">
  <br />
  <textarea name="list_urls" id="list_urls" cols="60" rows="20"></textarea>
  <br />
  <br />
  <input value="Open URLs" class="submit" type="button" onClick="open_all();">
  <br />
  <input type="reset" value="Reset!">
  <br/>
</form>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
180 views
Welcome To Ask or Share your Answers For Others

1 Answer

The issue is because your current code will add http:// to the start of any URL which starts with https://. You need to change your logic so that it checks for https:// at the start of the URL as well as http://. You can also tidy up the logic slightly and use trim() to ensure the line wasn't only whitespace. Try this:

function open_all() {
    var urls = document.getElementById("list_urls").value.split('
');
    for (var i = 0; i < urls.length; i++) {
        var url = urls[i];
        if (url.trim()) {
            if (s.substr(0,7) != 'http://' && s.substr(0,8) != 'https://') 
                url = 'http://' + url;
            window.open(url);
        }
    }
    return false;
}

Also note that you may have issues with your browser's popup blocker stopping you spamming the creation of so many windows so quickly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...