<a href="#" onclick="open_in_new_window('html_contents', 'MyTitle', 'location=1,status=1,toolbar=1,scrollbars=1,resizeable=1,width=500,height=250');">Open New Window</a>
<div id="html_contents" data-new-window> <!-- data-new-window is important -->
<video controls style="width: 100%; height: auto;">
<source src="http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4" type="video/mp4" />
</video>
</div>
CSS
[data-new-window] {
display: none;
}
JavaScript
function open_in_new_window(id, new_page_title, features) {
var new_window;
if(features !== undefined && features !== '') {
new_window = window.open('', '_blank', features);
}
else {
new_window = window.open('', '_blank');
}
var html_contents = document.getElementById(id);
if(html_contents !== null) {
new_window.document.write('<!doctype html><html><head><title>' + new_page_title + '</title><meta charset="UTF-8" /></head><body>' + html_contents.innerHTML + '</body></html>');
}
}
说明
#html_contents
存储新页面内容 (<video>
),使得你可以更轻松地操作(不需要在 JavaScript 中编写整个页面)。该函数需要参数 features
来定义新窗口(就像 window.open()
函数一样)。你只需要调用这个函数,如果定义了 features 参数,它将会在一个新的窗口中打开。如果没有定义,它将会在一个新的标签页中打开(或者在浏览器中定义的任何其他方式中打开)。
CSS 会在调用此函数的主窗口中隐藏 #html_contents
。
希望这对你有所帮助。