Javascript alert showing site url. How can i remov

2019-01-28 01:28发布

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.

Means redirect from abc.com to xyz.com and alert box showing xyz.com + msg ?

2条回答
男人必须洒脱
2楼-- · 2019-01-28 01:55

How can i remove site url from top of javascript alert?

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:

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 have to 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:

<input type='button' id='btnPopup' value='Click for Pop-Up'>

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:

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) { }
    }
  };
}

Live example

Obviously you'd want to tart that up a bit, and again, this is a bare-bones example, not a complete solution.

查看更多
Root(大扎)
3楼-- · 2019-01-28 01:55

@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;

Was testing it out on http://htmledit.squarefree.com, basically just type in the text box:

<script>alert('whatever');</script>

And the output is an alert box without the URL. Seems the work with most online html editors...

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>
查看更多
登录 后发表回答