如何在html和javascript中创建多个url开启者 [英] How to make multiple url opener in html and javascript

查看:77
本文介绍了如何在html和javascript中创建多个url开启者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTML和Javascript中也制作了多个网址开启者。我遇到一些HTTP链接打开但HTTPS链接不正常的问题。任何人都可以帮助我吗?这里是代码。



function open_all(){var url = document.getElementById(list_urls)。value; var urls = urls.split('\\\
'); var totalno = urls.length; var s; for(var i = 0; i

< form method = postaction => < br /> < textarea name =list_urlsid =list_urlscols =60rows =20>< / textarea> < br /> < br /> < input value =Open URLsclass =submittype =buttononClick =open_all();> < br /> < input type =resetvalue =Reset!> < br />< / form>

advance

解决方案

问题是因为您当前的代码会添加 http:// 开始以 https:// 开头的任何URL。您需要更改您的逻辑,以便在URL的开始处检查 https:// 以及 http:// 。您也可以稍微整理逻辑并使用 trim()来确保行不仅仅是空格。试试这个:

 函数open_all(){
var url = document.getElementById(list_urls).value。分裂( '\\\
');
for(var i = 0; i< urls.length; i ++){
var url = url [i];
if(url.trim()){
if(s.substr(0,7)!='http://'&& s.substr(0,8)!=' https://')
url ='http://'+ url;
window.open(url);
}
}
返回false;
}

另外请注意,您的浏览器的弹出式窗口拦截器可能会阻止您发送垃圾邮件很快创建这么多的窗口。


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('\n');
  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>

Thanks in advance

解决方案

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('\n');
    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.

这篇关于如何在html和javascript中创建多个url开启者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆