Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 766
SKU Avatar asked Oct 18 '11 05:10

SKU


People also ask

Can you customize JavaScript alert?

The finally negative to these native JavaScript alert dialogs is they cannot be styled or customized.

What can I use instead of JavaScript alert?

The alternative is a javascript 'modal window'. Having a google for that should turn up a plethora of options. This method enables you to style a div however you like and to have that shown in place of the alert box. If you've ever seen a lightbox, the effect and idea is very similar.

What is alert () in JavaScript?

One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen. Before this function can work, we must first call the showAlert() function. JavaScript functions are called in response to events.

Can you put HTML in a JavaScript alert?

You can add HTML into an alert string, but it will not render as HTML. It will just be displayed as a plain string.


2 Answers

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.

like image 119
T.J. Crowder Avatar answered Oct 05 '22 23:10

T.J. Crowder


@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>
like image 26
Ilan Kleiman Avatar answered Oct 05 '22 22:10

Ilan Kleiman