0

This is continuation of question asked here (Provide link to iframe via js) as a second part. Here I am opening my iframe link. The iframe link opens up a PDF. I am currently running the code in localhost server which is why the address is http://127.0.0.1:5500. But when I upload it to the main server, this will be changed. My question is, since the link of the PDF is stored in the same directory as the index.html page. How do I automatically generate http://127.0.0.1:5500 or any other address depending on the hosting server. Such that it could be www.myhostingserver.come/myID/PDFIni.pdf and I do not have to manually enter the hosting address, rather just give : 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";
        }
Arthur
  • 45
  • 5

1 Answers1

1

Try to use document.location.hostname. For example:

    var str = 'https://' + document.location.hostname + '/PDFIni.pdf';
    
    if(myIframe.src != str) {
        console.log(myIframe.src);
        myIframe.src = str;
    }
r1ddler
  • 178
  • 6
  • Hi I tied your solution but unfortunately the page does not load at all. Looks like it does not take the document.location.hostname. – Arthur Apr 08 '21 at 20:57
  • Try HTTP instead HTTPS, in my answer I used HTTPS, because thought you are using it – r1ddler Apr 08 '21 at 20:59
  • Thanks!! Http worked. But wouldn't it cause problems later depending on the server? – Arthur Apr 08 '21 at 21:03
  • Hmm, can't say for sure :( By the way, read [this](https://stackoverflow.com/questions/6725890/location-host-vs-location-hostname-and-cross-browser-compatibility) post about `document.location.host` and `document.location.hostname` . I think it will be helpful for you – r1ddler Apr 08 '21 at 21:05