There is website that alerts you with a text according to what you have done in the page.
I want to read that message with JavaScript so I can write some code according to what the page have shown in the popup text.
alert("message");
I just need to know what the "message" is!
The website that I'm trying to get message from is coded with asp.net.What should I do with that if it's impossible to read the message with JS.
alert() is a global function, ie window.alert() so can be overwritten.
Most likely, you'll still want the alert, so you can keep a record of it before overwriting, giving:
window.old_alert = window.alert;
window.alert = function(msg) {
// Process the msg here
console.log(msg);
// still show the original alert
old_alert(msg);
};
alert() function when executed is passed to the Browser to execute. Each browser executes it in its own way. So a way around is to override the alert() function itself.
Some javascript code on the page might be calling the alert() function. Maybe you can try to find the place in the code where it is called. The argument to alert() is what you need. You can override the default alert function using your own as described in: JavaScript: Overriding alert()
. So you can do (as taken from the above answer):
(function(proxied) {
window.alert = function() {
// do something here
// arguments is what holds what you want.
return proxied.apply(this, arguments);
};
})(window.alert);
#freedomn-m's answer is more relevant and apt. But you can use the answer for overriding alert() for more examples on how to do it.
Related
i am working in an angular project. In a controller, we have a button that is supposed to generate & copy an embed code (similar to youtube) to the clipboard. However, depending on the type of the item, the embed code can only be generated/returned by an ajax call. Have a look at this code:
function copyEmbed(e) {
var embedCode = '';
if (type === "typeA"){
api.items.compile.get({'id': item.selected.id},
function (response) {
embedCode = response.html; //<-- takes time to populate obviously
copyToClipboard();
});
} else {
embedCode = generateEmbedCodeTemplate(); //no ajax here. populates immediately
copyToClipboard();
}
function copyToClipboard() {
clipboard.copyText(); // all seems good but copying will fail as this function is not invoked with a click handler!
}
}
The problem is that because of the ajax call, the code to copy the resulting embed code cannot be in the copyEmbed function scope, as this means the ajax call will not have the time to get the data before copying. If i was able to make everything synchronous, i would be able to get the data and then call the copy command from within the scope of the copyEmbed function, so it would not fail, as the copyEmbed function is bound to a click event. However, in the example, i am handling the ajax call right, but the copyToClipboard function is not invoked with a click handler so the copy command fails. Any ideas, without resulting in hacky setIntervals to check for embedCode contents?
As always, handling async stuff can only happen by actually handling them in an async fashion. So instead of grabbing the embed code at the time of the button click, i am doing so way before, so it is available as a variable or a data-attribute of the element to be clicked. Only adding the answer for people who might come in here with the same brainfart i had when i opened it.
I am trying to call a method of a javascript from the actionscript using the ExternalInterface.
Here is the code in action script
private function onKickEvent(e:LogoutEvent):void{
ExternalInterface.call("LoginFound","message");
return;
}
And this is my javascript mwthod
function LoginFound(message){
alert(message);
anotherInstanceExists=true;
}
Everything is working fine, but the only thing is when act on the alert box which is shown in the javascript after some 20 secs, the exception is thrown from the flash player that a script has been running longer than expected time 15 sec.
How can i avoid this?
Best way to fix this issue is to add setTimeout inside your javascript on the alert line.
It should look like this:
setTimeout(function(){ alert(message) }, 1);
By doing it this way execution won't stop because of the alert.
When you call js function from the actionscript, that function have to work and return value not longer than in 15 sec. Javascript works in single thread,and when you call LoginFound function, alert stops farther executions on the thread.
function LoginFound(message){
alert('something');
//Nothing will be executed unless `alert` window will be closed
}
However you can handle such situation (the execution,which is longer than 15 sec) in Actionsript by using try/catch:
private function onKickEvent(e:LogoutEvent):void{
try{
ExternalInterface.call("LoginFound","message");
}catch(e:Error){
//Do something
}
}
I think your onKickEvent is called frequently
so that the javascript is called regularly. finally the browser timeout event
occurs. It always happen in recursive function.
I get an error message Error Message: Not implemented When doing this and using this statement in a server side include.
window.onload=readInSubsystemInformationFromFile();
It works however if I click ok button on that error message, its just annoying that this error message pops up.
So my question to you is, are there a function that checks if readInSubsystemInformationFromFile() has been initialized? If so do this method (In other words it shall be of the type onLoad) If not, then wait until it is ready.
Thanks in advance =)
To check if the function has been defined yet:
if(typeof readInSubsystemInformationFromFile == 'function'){
// Exists
readInSubsystemInformationFromFile();
}
Also, try jQuery for it's onload method... Works in pretty much all browsers and works well. It's a hard thing to get right.
$(document).ready(function(){
// Your code here
});
http://api.jquery.com/ready/
I'm binding the window.onload event like this
// It's a little more complex than this, I analyze if there is any other function
// attached but for the sake of the question it's ok, this behaves the same.
window.onload = myfunction;
Onload is triggered twice on my local machine a several times on the production server
If I change it by the jQuery equivalent
$jQuery(window).load(myfunction);
It behaves as expected (executed only once).
Could you help me to understand possible reasons why the first option it's not working as supposed?
Thanks!
The parentheses on your assignment — myfunction() — executes your function. You haven't shown what myfunction does, but this means that the return value from that function is being assigned to window.onload, not the function itself. So, I don't know how that is getting executed, unless you have somehow got that to work, like ending the function with return this;
You want
window.onload = myfunction;
Given the nature of window.onload, it seems unlikely that pure browser events alone are making both calls to myfunction. Therefore, a breakpoint inside your function will help you see the call stack. I've included screenshots for Chrome.
Sample code:
var alertme = function() {
alert("Hello");
}
window.onload = alertme;
function testsecondcall() {
alertme();
}
testsecondcall();
Open your page in Chrome.
After the page has loaded once, open the Developer Tools panel and put a breakpoint on the line inside your function, then refresh the page.
Check the call stack of both times that it breaks. One will be empty (the actual window.onload). The other should give you some information like the following:
On the right, under "Call Stack", you see alertme is called by testsecondcall
I've bound a function to a form submit button. I want the function to pop up an iframe dialog box and wait for user interaction, and THEN submit the form (via an ajax post).
But the behavior I'm seeing is that the dialog pops but the function continues executing and does the ajax post, even though the dialog box call hasn't returned yet. I need some way of halting the code execution until that dialog function returns.
I have something like:
submitForm: function () {
<call a function that pops up a dialog, doesn't return until user interacts>
new Ajax.Updater('dialogContainer', url, {
...........
}
});
return false;
}
I think this is the expected behavior of Javascript but am not sure how to get around it...
Javascript in the browser will execute sequentially, so obviously your call to launch the iframe dialog box doesn't wait for the user interaction & returns immediately. My guess is that whatever library you're using to popup your dialog box will have some sort of callback function that it will execute based on the user action instead. So you just need to move your new Ajax.Updater(....); code into a callback function.
Based on a basic knowledge that I have of javascript I think that's the normal behavior as long as the dialog box runs independently of your code (the code the called it).
Does your dialog box have any javascript running in it? Because I think, if it has, you can maybe pass a function as a parameter (callback that will execute the code you want) that will be called from the dialog box when it returns.
I would say that you should call the first function also through ajax.
and I think you are using prototype.
You have onComplete callback function in Ajax.Request and Ajax.Updater Objects.
Then in onComplete function can run the second function.
like
onComplete : function() {
new Ajax.Updater('dialogContainer', url, {
...........
}
});
}
If your target web client is Internet Explorer only, you can use window.showModalDialog. It has the nice property of blocking the javascript thread in the parent window.
showModalDialog has been implemented in Firefox, but I don't know if the function is blocking in Firefox.