简体   繁体   English

HTML从iframe Javascript获取Src ID

[英]Html Get Src Id From Iframe Javascript

Looking to find how to define a variable to only the numbers "1214693" in console based on the url withing the iframe, I've tried "document." 为了找到如何根据iframe的网址在控制台中将变量仅定义为数字“ 1214693”的方法,我尝试了“文档”。 but I almost never use html nor javascript so I'm really unsure of how to go about this. 但是我几乎从不使用html或javascript,所以我真的不确定该怎么做。

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

It's in the window object, not document . 它在window对象中,而不是document

Use this code in your iframe .html: 在iframe .html中使用以下代码:

console.log(window.location.href)

and it will log the URL. 它将记录URL。

Then it is a simple matter to tokenize URL and extract data: 然后,简单地处理令牌化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); } 

Not sure if this is what you're looking for but if you'd like the change the URL and have the id be parameterized then you'll want to do something like this: 不知道这是否是您要查找的内容,但是如果您想更改URL并对其ID进行参数化,则需要执行以下操作:

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

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

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