javascript 如何在弹出窗口中显示消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8197173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display a message in a pop up window
提问by BruceyBandit
I have a pop up window function where when a button is clicked the window does pop up. Now the pop up window does not display anything, for a test I want the pop up window to display the word "Session". But I don't know how to do this.
我有一个弹出窗口功能,当单击按钮时,窗口会弹出。现在弹出窗口不显示任何内容,为了进行测试,我希望弹出窗口显示“会话”一词。但我不知道如何做到这一点。
Am I supposed to write the word "Session" in another page and link to that page or is it possible to write the word "Session" on the same page and link it to that section of the page?
我应该在另一个页面上写下“会话”一词并链接到该页面,还是可以在同一页面上写下“会话”一词并将其链接到页面的该部分?
I want the latter to happen but I don't know how to do it.
我希望后者发生,但我不知道该怎么做。
Below is my code:
下面是我的代码:
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Create a Session</title>
<script type="text/javascript">
function openSessionPopup (session) {
window.open(session,
'window',
'width=500,height=500,scrollbars=yes,status=no');
}
</script>
</head>
<body>
<form action="create_session.php" method="post" name="sessionform">
<p><input class="questionBtn" type="button" value="Prepare Questions" name="prequestion" onClick="openSessionPopup(this.href); return false" /></p>
</form>
</body>
回答by icktoofay
Try something like this:
尝试这样的事情:
var win = window.open("about:blank", null, "width=400,height=300");
var doc = win.document;
doc.open("text/html");
doc.write("Session");
doc.close();
Try iton JSFiddle.
试试吧上的jsfiddle。
Alternatively, you can create a new page with the content you want and open that:
或者,您可以创建一个包含您想要的内容的新页面并打开它:
window.open("my-new-page.html", null, "width=400,height=300");
回答by David Bélanger
If you want to write the word session into the popup.. be sure to use " before and after since it's string... like this window.open("session", 'window', ...
如果你想把 session 这个词写到弹出窗口中.. 一定要使用 " before and after 因为它是字符串......就像这样 window.open("session", 'window', ...
" or ' will do the job either way.
" 或 ' 会以任何一种方式完成工作。
回答by Ashok Padmanabhan
Your popup typically displays another page when it appears. It is this other page that will have the content you want displayed.
您的弹出窗口出现时通常会显示另一个页面。正是这个其他页面将显示您想要显示的内容。
回答by Marc B
This code would never work. When you pass in this.href
, the this
refers to the input element on which you've clicked. Input elements do not have an 'href' attribute, so you're passing in an undefined value.
这段代码永远不会工作。当您传入 时this.href
,this
指的是您单击的输入元素。输入元素没有“href”属性,因此您传入的是未定义的值。
回答by Vladimir Posvistelik
First parameter in windows.open
function is URL. So when you passed an empty value it opened the window with url about:blank
(empty page).
windows.open
函数中的第一个参数是 URL。因此,当您传递一个空值时,它会打开带有 url about:blank
(空页面)的窗口。
If you want open url http://example.com/in popup window you must use your function as:
openSessionPopup('http://example.com/');
如果你想在弹出窗口中打开 url http://example.com/你必须使用你的函数:
openSessionPopup('http://example.com/');
回答by Chris
You can pass the href as the argument for the "session" parameter. You see this works by putting this code into a file called "question.html" and testing out the button.
您可以将 href 作为“会话”参数的参数传递。您可以通过将此代码放入名为“question.html”的文件并测试按钮来查看此方法。
Replace the href with the page you want to display.
将 href 替换为您要显示的页面。
onClick="openSessionPopup('question.html'); return false"
onClick="openSessionPopup('question.html'); return false"