問題描述
為什麼 Internet URL 在 iframe 標記中不起作用? (Why is an Internet URL not working in an iframe tag?)
我的代碼適用於本地 URL,但不適用於 Internet URL:
<div style="margin‑top: 8px; display: Block">
<input type="text" class="form‑control input‑sm" id="Txtlink" placeholder="Enter or Paste a Link" style="width: 84%; float: left;"><a style="border‑radius: 5px; float: right" class="btn btn‑primary" onclick="isValidURL()" type="button">Add</a>
<iframe id="urliframe" src="" width="404" height="250" scrolling="no" style="display:none;">
<p>Your browser does not support iframes.</p>
</iframe>
</div>
jQuery:
function isValidURL() {
var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0‑9]+)?(\/|\/([\w#!:.?+=&%@!\‑\/]))?/;
var url = document.getElementById("Txtlink").value
if (RegExp.test(url)) {
$(document).ready(function () {
$("#urliframe").show();
var loc = url;
alert(loc);
document.getElementById('urliframe').setAttribute('src', loc);
});
}
else {
alert("Error");
}
}
參考解法
方法 1:
If it's a 3rd party site, they may have a denied access by using the X‑FRAME‑OPTIONS header.
https://developer.mozilla.org/en‑US/docs/Web/HTTP/X‑Frame‑Options
If it is your site, you may need to set the correct header to allow the page to be visible within the frame.
(by solanki kaushik、lysp)