【问题标题】:how to modify just protocol of url in a iframe tag with jquery如何使用jquery修改iframe标签中的url协议
【发布时间】:2018-05-02 21:50:08
【问题描述】:

我需要使用 jquery 将通过 iframes 标签加载的 youtube 视频的“http”协议更改为“https”协议。

这是我目前的代码

$(document).ready(function(){
    var protocol = "http";
    var url = $('body').find('iframe').attr('src');
    if (protocol.test(url)) { // if we find http in src frame's atribute
        //then I replace just that part, for "https"
    }
});

有什么想法吗?

【问题讨论】:

  • 我猜想绕过 https/http 警告为时已晚。 (猜猜你为什么这样做)
  • 所以匹配 http: 并替换它。基本正则表达式。

标签: javascript jquery https


【解决方案1】:

您只需要测试一下http:// 是否包含在 iframe 源 URL 中。如果包含在源 URL 中,则将 http:// 替换为 https:// 并设置新的 src URL。

$(document).ready(function(){
    var iframe = $('body').find('iframe');
    var url = iframe.attr('src');
    if (url.includes("http://")) {
        iframe.attr('src', url.replace("http://", "https://")); 
    }
});

【讨论】:

    【解决方案2】:

    您可以使用正则表达式直接自动替换http://

    $(document).ready(function(){
        var iframe = $('body').find('iframe');
        var url = iframe.attr('src');
        url = url.replace(/^http:\/\//i, 'https://');
        iframe.attr('src',url);
    });
    

    【讨论】:

      猜你喜欢
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 2013-02-25
      • 1970-01-01
      • 2014-03-11
      • 2013-01-21
      相关资源
      最近更新 更多