Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iframe中的javascript不工作_Javascript_Iframe - Fatal编程技术网

iframe中的javascript不工作

iframe中的javascript不工作,javascript,iframe,Javascript,Iframe,我使用的URL类似于: 我试图在iframe中使用javascript嵌入youtube视频: <iframe width="560" height="315" src="https://www.youtube.com/embed/<script type="text/javascript"> var segment_str = window.location.pathname; var segment_array = segment_str.split( '/' ); var

我使用的URL类似于: 我试图在iframe中使用javascript嵌入youtube视频:

<iframe width="560" height="315" src="https://www.youtube.com/embed/<script type="text/javascript">
var segment_str = window.location.pathname;
var segment_array = segment_str.split( '/' );
var last_segment = segment_array.pop();
document.write(last_segment); 
</script>" frameborder="0" allow="autoplay; encrypted-media"  allowfullscreen></iframe>

它应该给出src=”https://www.youtube.com/embed/GF60Iuh643I"

但是javascript仍然没有执行, 你知道怎么做吗

谢谢大家!

试试这个(未测试)。通过id获取iframe,然后将src设置为等于链接+最后一段

<iframe id="videoframe" width="560" height="315" src="" frameborder="0" allow="autoplay; encrypted-media"  allowfullscreen></iframe>

<script type="text/javascript">
var segment_str = window.location.pathname;
var segment_array = segment_str.split( '/' );
var last_segment = segment_array.pop();
document.getElementById('videoframe').src = 'https://www.youtube.com/embed/' + last_segment;
</script>

var segment_str=window.location.pathname;
变量段数组=段字符串拆分('/');
var last_segment=segment_array.pop();
document.getElementById('videoframe').src='10〕https://www.youtube.com/embed/“+最后一段;

代码中的主要问题是不能像这样在iframe元素中添加js脚本。很高兴,我能帮上忙。