简体   繁体   中英

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." but I almost never use html nor javascript so I'm really unsure of how to go about this.

 <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 .

Use this code in your iframe .html:

console.log(window.location.href)

and it will log the URL.

Then it is a simple matter to tokenize URL and extract data:

 // 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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