Christina
Christina

Reputation: 189

whats the alternate for alert function in javascript?

whats the alternate for alert() function in java script?

I do not want to use the alert function as it will display some message.

I just want to activate a function, when i use alert() the function gets activates and show the result otherwise it doesnt show anything.

Please help!

    <script type="text/javascript">

(function () {
    var test = document.createElement('script'); 
    test.type = 'text/javascript'; test.async = true;
    test.src = 'http://mysite.com/plugin/myscript.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(test, s);
  alert("hi");

})();

    </script>

so the above code runs only if i add alert("hi") in it, otherwise it wont. so how to activate the myscript.js code

Upvotes: 1

Views: 8195

Answers (8)

Javascript-GameDev
Javascript-GameDev

Reputation: 23

let displayedText = '';

// Define div in html a visble div with width and height.
let divForTextToDisplay = document.querySelector('TextDiv');

// Replace with your own loop
function GameLoop() {
  divForTextToDisplay.innerHTML = `${displayedText}`;
}

window.onload = function() {
  setInterval(GameLoop,1000/30);
  blurt('This is an example Text',2000,500)
}


// Extra time is how long the text should stay on screen after completing
function blurt(text, maxTime, extraTime) {
  displayedText = ''
  let induvidualLetterTime = maxTime/text.length;
  for (let i = 0; i < text.length; i++) {
    setTimeout(() => {
      displayedText += text[i];
    }, induvidualLetterTime * i);
 }
 setTimeout(() => {
    displayedText = ''
 }, maxTime + extraTime);
}

Displays text in a div (Need to define using HTML and CSS)

Upvotes: 0

Hamza Fakhir
Hamza Fakhir

Reputation: 1

you can use prompt or confirm

<script type="text/javascript">

    ...
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(test, s);
    confirm("hi");

    })();

    </script>

Upvotes: 0

Aesthete
Aesthete

Reputation: 18848

Sure I guess..

alert = function(msg) {};

Now alert does nothing, and will not display a popup dialogue.

If you wanted to divert any message bound for the alert dialogue to your own function, say - a logger - you could do something like this:

function log(msg) {
  console.log(msg);
  yourOwnFunction(); // You can call and functions if you want.
};

alert = log;
alert("Error!");

"Error!" now appears in the console log instead of a popup.

Edit

All your strings here are literal anyway, why don't you just put this before your script:

<script type="text/javascript" src="http://mysite.com/plugin/myscript.js" async="async"></script>

Upvotes: 4

davids
davids

Reputation: 6371

Well, if you want to see the result of a function but don't want to see it as an alert, try console.log(), which will show the output in the javascript console

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328724

There is no reason to use alert in 2012. You have plenty of other options:

  1. The browser's console. Try console.log()
  2. Use a logging framework. Here is a list. They usually display a div somewhere where the log messages will show up plus plenty of options to filter them.

Upvotes: 1

RRikesh
RRikesh

Reputation: 14381

Are you looking for an alternative for alert()? Try console.log();

Upvotes: 3

David Hedlund
David Hedlund

Reputation: 129802

You can overwrite alert with whatever you want

window.alert = function(x) {
    customMessageDialog(x);
};

Note that you will not be able to simulate the script-blocking behavior of alert, so it might not be safe to replace.

If you just want to do something additional every time an alert is raised, but still use the alert and its blocking qualities, you could do something like this:

window.alertOld = window.alert;
window.alert = function(x) {
    customAction(x);
    window.alertOld(x);
};

Upvotes: 2

Jithin Jose
Jithin Jose

Reputation: 1821

Try this

 window.alert = function(x) {
        // this function executed when you call alert function;
        // x=>message given in alert function
 }

Upvotes: 2

Related Questions