2015-10-09 141 views
0

寻找如何在控制台中根据带有iframe的url定义一个变量,只有数字“1214693”,我试过“文档”。但我几乎从来没有使用HTML或JavaScript,所以我真的不确定如何去做这件事。Html从iframe获取Src ID Javascript

<iframe src="/build/upload?groupId=1214693" id="upload-iframe" frameBorder="0" scrolling="no" style="width:100%; height:175px; margin-left:10px"></iframe>

回答

1

它在window对象,而不是document

使用此代码在您的IFRAME的.html:

console.log(window.location.href) 

,它会记录该URL。

然后,它是一个简单的事情来标记URL和提取数据:

// thanks to http://stackoverflow.com/questions/11582512/how-to-get-url-parameters-with-javascript/11582513#11582513  
 
function getURLParameter(name) { 
 
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null 
 
} 
 
if (window.console) { 
 
    var idParam = getURLParameter('id'); 
 
    console.log(idParam); 
 
}

0

不知道这是你在找什么,但如果你想改变的URL,并将参数设置为id,那么您将需要这样做:

<script type="text/javascript"> 
var id = 1214693; 
$(document).ready(function() { 
$('#iframe').attr('src', '/build/upload?groupId=' + id); 
}) 
</script>