var yourName; //global variable accessible to all functions
function showAnotherMessage() {
alert("Hi " + yourName ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file");
}
function init() {
yourName = Prompt("Hi. Enter your name.\nWhen the browser window is first loaded\nthe function containing this prompt window is called.", "Your name");
clickme = document.getElementById("clickme");
clickme.onclick = showAnotherMessage;
}
window.onload = init();
function showAnotherMessage() {
alert("Hi " + yourName + ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file");
}
you miss a "+" in your alert.
You are missing a + in your alert concatenation.
alert("Hi " + yourName + ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file");
Pass to the window.onload not the result of the function, but the reference of the function and add missed + in the alert's message
alert("Hi " + yourName + ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file");
Reference
window.onload = init;
You are missing a concatenation.
Prompt should be in lower case.
var yourName; //global variable accessible to all functions
function showAnotherMessage() {
alert("Hi " + yourName + ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file");
}
function init() {
yourName = prompt("Hi. Enter your name.\nWhen the browser window is first loaded\nthe function containing this prompt window is called.", "Your name");
clickme = document.getElementById("clickme");
clickme.onclick = showAnotherMessage;
}
window.onload = init();
if the issue is still there,, then it is related to calling the function
clickme.onclick = showAnotherMessage;
// instead use below
clickme.onclick = showAnotherMessage();
Related
I have this function
function runcode(code) {
var tag = document.createElement("script");
document.body.appendChild(tag);
tag.innerHTML = code;
}
And when I call this
certainValue = "test";
anotherValue = "alert('hi');";
runcode("function " + certainValue + "() {" + anotherValue + "}");
And then call this in the Chrome J's console
test
It tells me the function code. But when I call
test();
nothing happens. Why?
I have a button that executes a function:
$("#btnRemove").click(function () {
var name= $("#editAccountName").val();
if (confirm("Are you sure you want to mark " + "''" + name + "''" + " as innactive?")) {
saveAccount(false);
window.location.href = "/RxCard/Search";
}
alert (name + "was marked innactive.")
});
I need the alert to show after the user is redirected to "/Rxcard/Search"
what do i need to change in my code to get it working like that?
on a side note, how would do the same but with a CSS customized alert?
Thanks.
Instead of putting your alert in this code, you need to put it into the script behind Search page. Now you can add a url parameter and then in there check it and show the alert if that parameter is set:
if (confirm("Are you sure you want to mark " + "''" + name + "''" + " as innactive?")) {
saveAccount(false);
window.location.href = "/RxCard/Search?name=" + name;
}
And then add this somewhere (doesn't matter that much):
$.urlParam = function(name){
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results==null){
return null;
}
else{
return results[1] || 0;
}
}
And at last this code goes into your search page code:
function() {
if($.urlParam('name') == true){
alert (name + "was marked innactive.");
}
}();
You cannot run an alert after the location.href has changed because it causes the browser to refresh. Once refreshed, your script is no longer running.
You would need to move your alert script into your search page and perhaps pass the name as a querystring arguement.
You could store the name value using localstorage. The value can be evaluated after the redirection so you can display the dialog with the stored value (if any)
You can't style your alert dialog but you can always create a modal dialog from scratch or by using a web framework / library.
Consider the following HTML snippet containing some javascript utilizing prompt and unload. The prompt() method works fine but I want alerting something like Goodbye, user when reloading or leaving the page. Any help is greatly appreciated.
<html>
<body onload="promptName()" >
<script type="text/javascript">
function promptName()
{
var userName = prompt("What's your name ?", "")
return userName;
}
function goodBye()
{
alert("Goodbye, " + promptName() + "!");
}
window.onunload = goodBye;
window.onbeforeunload = goodBye;
</script>
</body>
</html>
try this
<script type="text/javascript">
var userName;
function promptName()
{
userName = prompt("What's your name ?", "")
return userName;
}
function goodBye()
{
//alert("Goodbye, " + promptName() + "!");
return("Goodbye, " + userName + "!");
}
window.onunload = goodBye;
window.onbeforeunload = goodBye;
</script>
stackoverflow
I need to get text from alert box.
<script type="text/javascript">
alert('Some text')
</script>
I don't have enough reputation to upload images..so i upload code instead of image :)
Is there any way to get text "from popup" on Chrome using Greasemonkey?
Query is not clear ..but if I understand it correctly ..there is a JavaScript on a page that results in an alert()
You can get the text of that alert from the JavaScript on the page.
function getAlert() {
// get all scripts
var elem = document.scripts;
// loop and check
for (var i = 0, len = elem.length; i < len; i++) {
var txt = elem[i].textContent.match(/alert\(['"]([^'"]+)['"]\)/);
if (txt) { return txt[1]; } // if matched, return the alert text and stop
}
}
In your case, the above function will return Some text.
Here's another interesting way (if you don't mind overriding the global alert, prompt & cancel functions)...
// Wrap the original window.alert function...
const windowAlert = window.alert;
window.alert = function(message) {
console.log(`window.alert called with message: ${message}`);
return windowAlert(message);
};
alert('FOO');
// Console Output:
// window.alert called with message: FOO
// ========================================================
// Wrap the original window.prompt function...
const windowPrompt = window.prompt;
window.prompt = function(message) {
console.log(`window.prompt called with message: ${message}`);
const input = windowPrompt(message);
console.log(`user entered: ${input}`);
return input;
};
prompt('BAR');
// Console Output:
// window.prompt called with message: BAR
// user entered: xxx
// ========================================================
// Wrap the original window.confirm function...
const windowConfirm = window.confirm;
window.confirm = function(message) {
console.log(`window.confirm called with message: ${message}`);
const choice = windowConfirm(message) ? 'ok' : 'cancel';
console.log(`user clicked: ${choice}`);
return choice;
};
confirm('BAZ');
// Console Output:
// window.confirm called with message: BAZ
// user clicked: ok (or 'cancel' if you click 'cancel')
Get user input from a prompt
var name = prompt('Please enter your name');
if (person!=null)
console.log('The person's name is: ' + name);
Get a yes/no answer
var response = confirm('Press a button');
if (response == true)
console.log("You pressed OK!");
else
console.log("You pressed Cancel!");
Can't seem to get this javascript redirect to work?
Suggestions?
Should I maybe do it with a meta refresh and how?
// If we have a successful location update
function onGeoSuccess(event)
{
document.getElementById("Latitude").value = event.coords.latitude;
document.getElementById("Longitude").value = event.coords.longitude;
document.getElementById("location").href = "track.cfm?track=s&Lat=" + event.coords.latitude + "&Long=" + event.coords.longitude;
var redirectUrl = "track.cfm?track=s&Lat=" + event.coords.latitude + "&Long=" + event.coords.longitude;
}
// If something has gone wrong with the geolocation request
function onGeoError(event)
{
alert("Error code " + event.code + ". " + event.message);
}
function redirect()
{
window.location = redirectUrl;
}
setTimeout(redirect,15000);
The problem is the scope of redirectUrl variable. You declared redirectUrl as local for onGeoSuccess function, so it will be visible only inside of it. For workaround ,you can put all this stuff:
function redirect()
{
window.location = redirectUrl;
}
setTimeout(redirect,15000);
inside of onGeoSuccess function, or make redirectUrl global, by removing var before redirectUrl:
redirectUrl = "track.cfm?track=s&Lat="+...
//^-----no 'var'
Declare redirectUrl in parent scope, and run onGeoSuccess function.
var redirectUrl;
function onGeoSuccess (event) {
document.getElementById("Latitude").value = event.coords.latitude;
document.getElementById("Longitude").value = event.coords.longitude;
document.getElementById("location").href = "track.cfm?track=s&Lat=" + event.coords.latitude + "&Long=" + event.coords.longitude;
redirectUrl = "track.cfm?track=s&Lat=" + event.coords.latitude + "&Long=" + event.coords.longitude;
}
function onGeoError (event) {
alert("Error code " + event.code + ". " + event.message);
}
function redirect () {
window.location = redirectUrl;
}
onGeoSuccess(...);
setTimeout(redirect, 15000);
Two problems:
1) The redirect URL is being set by a function which, at least from the code you posted, is not being called
2) Even if it was called, redirectUrl is a local variable to that function, so the timeout cannot access it.
Always check the error console.