How to display HTML Content using window.showModalDialog()

那年仲夏 提交于 2019-12-07 13:59:49

问题


Will I be able to display a HTML content (not a file) in a pop up window using JS?

I am trying to open a pop up window and display an user defined HTML inside this. I am trying the below, but it doesn't work.

window.showModalDialog("<p>Pop up window text</p>","resizable: yes");

Can some one suggest me how to do this? Thanks in advance.


回答1:


Contrary to window.open(), the first (URL) argument of showModalDialog() is required. Thus, you can't pass it an HTML string. You can either use window.open:

var newWindow = window.open("", "newWindow", "resizable=yes");
newWindow.document.write('<p>Pop up window text</p>');

Or alternatively, use one the existing modal plugins, such as jQuery UI Dialog or Twitter Bootstrap Modals. They allow you to easily display a modal window based on the content of an existing HTML element, e.g.:

<div id="dialog">
  <p>Pop up window text</p>
</div>

$('#dialog').modal(); // Twitter Bootstrap
$("#dialog").dialog({ resizable: true }); // jQuery UI


来源:https://stackoverflow.com/questions/12036329/how-to-display-html-content-using-window-showmodaldialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!