简体   繁体   English

如何更改iframe src

[英]How to change iframe src

Please bear with me if i'm asking something very basic as I don't know much about coding. 如果我问的是非常基本的问题,请耐心等待,因为我对编码不太了解。 I have a stock charting application which has a built-in web browser. 我有一个带有内置Web浏览器的股票图表应用程序。 I configured this browser to load a html page, which i coded as below, like this mypage.html?symbol=xzy, What i am trying to do here is to capture the submitted html variable, 'symbol' and use its value as part of the url string to load a portion of another third party webpage. 我配置了此浏览器以加载一个html页面,我将其编码如下,例如mypage.html?symbol = xzy,我在这里要做的是捕获提交的html变量'symbol'并将其值用作一部分url字符串以加载另一个第三方网页的一部分。 I can't figure out what's wrong with my javascript code that is failing to set the value of the 'src' attribute. 我无法弄清楚我的JavaScript代码出了什么问题,原因是无法设置'src'属性的值。 Any help is most appreciated. 非常感谢您的帮助。

<html>
<header>
<script>
document.getElementById('iframe').src = "http://www.asx.com.au/asx/markets/dividends.do?by=asxCodes&asxCodes="+symbol+"&view=all#dividends";
</script>
</header>
<body>
<div id="dividends"></div>
<iframe id="iframe" src="" style="display:hidden;margin-left: -140px; margin-top: -33px"    scrolling="no" frameborder="no" width="398" height="265"></iframe>
</body>
</html>

try move the script after html DOM 尝试在html DOM之后移动script

<body>
<div id="dividends"></div>
<iframe id="iframe" src="" style="display:hidden;margin-left: -140px; margin-top: -33px"    scrolling="no" frameborder="no" width="398" height="265"></iframe>
</body>

<script>
document.getElementById('iframe').src = "http://www.asx.com.au/asx/markets/dividends.do?by=asxCodes&asxCodes="+symbol+"&view=all#dividends";
</script>

Move the javascript to the bottom of the page AFTER the iframe. 在iframe之后,将javascript移至页面底部。 You are trying to change an element before it is created. 您正在尝试在元素创建之前对其进行更改。 Also do src="about:blank" initially, so it validates. 最初还要执行src =“ about:blank”,以便进行验证。

A couple of things: 1) start with performing the function onLoad - that way you know the script will run and popluate the src attribute after the browser renders it, and not before. 有两件事情:1)首先执行onLoad函数-这样,您就知道脚本将在浏览器渲染之后而不是之前运行并填充src属性。

2) use a function to get the url value. 2)使用一个函数来获取URL值。

<html>
<header>
<script>
<!-- source: http://javascriptproductivity.blogspot.com/2013/02/get-url-variables-with-javascript.html -->
function GetUrlValue(VarSearch){
    var SearchString = window.location.search.substring(1);
    var VariableArray = SearchString.split('&');
    for(var i = 0; i < VariableArray.length; i++){
        var KeyValuePair = VariableArray[i].split('=');
        if(KeyValuePair[0] == VarSearch){
        return KeyValuePair[1];
        }
    }
}

function SetContent(){
    var symbol = GetUrlValue('symbol');
document.getElementById('iframe').src = "http://www.asx.com.au/asx/markets/dividends.do?by=asxCodes&asxCodes="+symbol+"&view=all#dividends";
}

</script>
</header>
<body onLoad="SetContent();">
<div id="dividends"></div>
<iframe id="iframe" src="" style="display:hidden;margin-left: -140px; margin-top: -33px"    scrolling="no" frameborder="no" width="398" height="265"></iframe>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM