显示站点 url 的 Javascript 警报。如何从 javascript 警报顶部删除站点 url?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7802942/
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
Javascript alert showing site url. How can i remove site url from top of javascript alert?
提问by SKU
javascript alert box showing url + msg. i have not passed any thing but it shows url on the top of alert box. how can i remove this url ? The url is showing when my site is redirected from another site.
显示 url + msg 的 javascript 警报框。我没有通过任何东西,但它在警告框的顶部显示了 url。我怎样才能删除这个网址?当我的网站从另一个网站重定向时,该 url 会显示。
Means redirect from abc.com to xyz.com and alert box showing xyz.com + msg ?
意味着从 abc.com 重定向到 xyz.com 和显示 xyz.com + msg 的警报框?
采纳答案by T.J. Crowder
How can i remove site url from top of javascript alert?
如何从 javascript 警报顶部删除站点 url?
You can't. The browser adds that so that the user knows that the message isn't from the browser itself.
你不能。浏览器添加它,以便用户知道消息不是来自浏览器本身。
The only thing you can do is stop using alert
entirely, and instead use modern techniques for overlaying an element on top of the page. You can write this yourself, or use any of the many "lightbox" libraries out there. Note that doing so changes how you code things a bit, because while alert
(and confirm
and prompt
) bring execution of scripts on the page to a screeching halt while they're on-screen, you can't do that in your own code, so you have to structure your code to continue when it receives the event that the user dismissed the box. Usually the library will give you a callback you can use for that, so for instance this alert
code:
您唯一能做的就是alert
完全停止使用,而是使用现代技术在页面顶部覆盖元素。您可以自己编写,也可以使用许多“灯箱”库中的任何一个。请注意,这样做会稍微改变您编写代码的方式,因为 while alert
(和confirm
和prompt
)会使页面上的脚本在屏幕上显示时突然停止,您无法在自己的代码中执行此操作,因此您必须构建您的代码以在收到用户关闭框的事件时继续。通常库会给你一个回调,你可以使用它,例如这个alert
代码:
function foo() {
doSomething();
alert("Foo!");
if (x != 42) {
doSomethingElse();
}
}
becomes
变成
function foo() {
doSomething();
someNiftyLightboxThingy("Foo!", function() {
if (x != 42) {
doSomethingElse();
}
});
}
Not hard, you just have to get used to it.
不难,你只需要习惯它。
You don't haveto use a library for this, but doing so lets you not worry about edge cases and may offer you more features. Here's a fairly bare-bones example:
你不具备使用库这一点,但这样做可以让你不必担心边界情况,并可以为您提供更多的功能。这是一个相当简单的例子:
HTML:
HTML:
<input type='button' id='btnPopup' value='Click for Pop-Up'>
CSS:
CSS:
iframe.shim {
width: 100%;
height: 100%;
position: absolute;
left: 0px;
top: 0px;
z-index: 100;
background-color: #fff;
opacity: 0.5; /* Standard */
filter: alpha(opacity = 50); /* For IE */
}
div.overlay {
z-index: 101;
border: 1px solid black;
background-color: #ecc;
position: absolute;
left: 100px;
top: 100px;
}
JavaScript:
JavaScript:
function doVerySimplePopup(text, callback) {
var shim, div, parent = document.body;
shim = document.createElement('iframe');
shim.className = 'shim';
parent.appendChild(shim);
div = document.createElement('div');
div.className = 'overlay';
div.innerHTML = text;
parent.appendChild(div);
div.onclick = function() {
parent.removeChild(div);
parent.removeChild(shim);
if (typeof callback === "function") {
try { callback(); } catch (e) { }
}
};
}
Obviously you'd want to tart that up a bit, and again, this is a bare-bones example, not a complete solution.
显然,您想稍微强调一下,同样,这是一个简单的示例,而不是完整的解决方案。
回答by Ilan Kleiman
@T.J. Crowder's answer is correct under normal circumstances. Although not completely. There are ways to bypass this, and get what you want in the end;
@TJ Crowder 的回答在正常情况下是正确的。虽然不完全。有办法绕过这个,最终得到你想要的;
Was testing it out on http://htmledit.squarefree.com, basically just type in the text box:
在http://htmledit.squarefree.com上进行测试,基本上只需在文本框中输入:
<script>alert('whatever');</script>
And the output is an alert box without the URL. Seems the work with most online html editors...
输出是一个没有 URL 的警告框。似乎适用于大多数在线 html 编辑器...
This is simplified;
这是简化的;
<iframe id="myIframe" style="display:none;"></iframe>
<script>
var ifrm = document.getElementById('myIframe');
ifrm = ifrm.contentWindow || ifrm.contentDocument.document || ifrm.contentDocument;
ifrm.document.open();
ifrm.document.write('<script>alert("hello world");<\/script>');
ifrm.document.close();
</script>