【问题标题】:Get current directory address as localhost获取当前目录地址为 localhost
【发布时间】:2021-07-04 18:22:21
【问题描述】:

这是此处 (Provide link to iframe via js) 提出的问题的第二部分。在这里,我打开我的 iframe 链接。 iframe 链接打开一个 PDF。我目前正在 localhost 服务器中运行代码,这就是地址为 http://127.0.0.1:5500 的原因。但是当我将它上传到主服务器时,这将改变。我的问题是,因为 PDF 的链接存储在与 index.html 页面相同的目录中。如何根据托管服务器自动生成http://127.0.0.1:5500 或任何其他地址。这样它可以是www.myhostingserver.come/myID/PDFIni.pdf,我不必手动输入托管地址,而只需给出:hostingaddress+"/PDFIni.pdf"?

<script>
...
...

let myIframe = document.getElementById("iframe");
        
        if(myIframe.src != "http://127.0.0.1:5500/PDFIni.pdf")
        {
            console.log(myIframe.src);
            myIframe.src = "http://127.0.0.1:5500/PDFIni.pdf";
        }

【问题讨论】:

    标签: html iframe server


    【解决方案1】:

    尝试使用document.location.hostname。例如:

        var str = 'https://' + document.location.hostname + '/PDFIni.pdf';
        
        if(myIframe.src != str) {
            console.log(myIframe.src);
            myIframe.src = str;
        }
    

    【讨论】:

    • 您好,我绑定了您的解决方案,但不幸的是该页面根本无法加载。看起来它不需要document.location.hostname。
    • 尝试使用 HTTP 而不是 HTTPS,在我的回答中我使用了 HTTPS,因为我认为你正在使用它
    • 谢谢!! Http工作。但是以后根据服务器不会出问题吗?
    • 嗯,不能肯定:(顺便说一下,阅读this关于document.location.hostdocument.location.hostname的帖子。我认为这对你会有帮助
    猜你喜欢
    • 2017-02-10
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2016-08-20
    相关资源
    最近更新 更多