473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Less typing with alert

When testing with alert-boxes I got tired of writing the same name twice
like this:

alert ('variablename =' + variablename);

and wrote a helper function :

var alertt = function (a) {
var result = 'Variable name and value\n';
for (var i = 0; i < arguments.length;i += 1) {
result += arguments[i] + ' = ' + eval(arguments[i]) + '\n';
}
alert(result);
return result;
}

// testing the above function ------------------------
var ss = 'something';
var number = 3;
var b = true;
var scope = this;

alertt('ss','number','document.lastModified','Math .random()','b','scope');

// the above code creates an alert box like this (tested in ie6.0, ff3.01,
opera9.02 only)
Variable name and value
ss = something
number = 3
document.lastModified = 09/01/2008 21:24:00
Math.random() = 0.39872433505681937
b = true
scope = [object Window]

--------------------------

Do you find even easier way, where one could omit quotes?
Also other improvements are welcome.

Sep 1 '08 #1
6 934
On Sep 1, 8:41 pm, "optimistx" <optimistxPoi...@poistahotmail.com>
wrote:
When testing with alert-boxes I got tired of writing the same name twice
like this:

alert ('variablename =' + variablename);

and wrote a helper function :
Great idea!

Maybe this is a dumb question but what is the purpose of the "a" in
the following line?
var alertt = function (a) {
var result = 'Variable name and value\n';
for (var i = 0; i < arguments.length;i += 1) {
result += arguments[i] + ' = ' + eval(arguments[i]) + '\n';}

alert(result);
return result;

}
Sep 1 '08 #2
Yes, a great idea.

One can get rid of most of the quotes at the cost of adding a split
function.

<script type="text/javascript"><!--
function alertt(a) {
arg=a.split(",")
var result = 'Variable name and value~\n'
for (var i=0; i<arg.length; i++) {
result += arg[i] + ' = ' + eval(arg[i])+'~\n' }
alert(result) }

// testing the above function ------------------------
var ss = 'something ';
var number = 3;
var b = true;
var scope = this;
--></script>
<body
onload='alertt("ss,number,document.lastModified,Ma th.random(),b,scope")'>
I like an extra character at the end so one can see trailing blanks.

Perhaps this could be done with some sort of array and avoid the split
operation?

Sep 2 '08 #3
Steve wrote:
...
what is the purpose of the "a" in
the following line?
>var alertt = function (a) {
var result = 'Variable name and value\n';
for (var i = 0; i < arguments.length;i += 1) {
result += arguments[i] + ' = ' + eval(arguments[i]) + '\n';}

alert(result);
return result;

}
Thank you for looking at the code.

Variable a in argument list is unnecessary, remaining from earlier
one-variable version on of alertt:

var alertt = function (a) {
alert (a + ' = ' + eval(a) + '\n');
}

On further testing of this function there was a disappointment:
only variables in the global scope can be used, because eval()
works in global scope. Perhaps a modification to handle scope could
be found.

Also if the characters between quotes are not a variable name in global
scope then alertt behaves very badly (perhaps adding
try ...catch would help).

The growing length of alertt is not important
because it is used in testing only.
Sep 2 '08 #4
optimistx wrote on 02 sep 2008 in comp.lang.javascript:
On further testing of this function there was a disappointment:
only variables in the global scope can be used, because eval()
works in global scope. Perhaps a modification to handle scope could
be found.
As usual eval is evil.

The serverside version behaves as expected.

A simplified version for a single argument,
written in ASP-js:

====================================
<script type="text/javascript">

var test = 123;

function f1() {
<%= alertt("test") %>
};

function f2() {
var test = 456;
<%= alertt("test") %>
};

</script>

<body
onload='f1();f2()'>
<script language='javascript' runat='server'>
function alertt(x){
response.write("alert('"+x+" = '+"+x+");");
};
</script>
====================================

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 2 '08 #5
On Sep 1, 9:31*pm, "optimistx" wrote:
On further testing of this function there was a disappointment:
only variables in the global scope can be used, because eval()
works in global scope. Perhaps a modification to handle scope could
be found.

Also if the characters between quotes are not a variable name in global
scope then alertt behaves very badly *(perhaps adding
try ...catch would help).

The growing length of alertt is not important
because it is used in testing only.
As Evertjan said: eval is evil, and now it seems also inconvenient for
this function. I think it's better to pass the variables themselves as
opposed to a string which needs to be eval'ed. This is my attempt at
the function:

function alertt(a) {
var result = 'Variable name and value\n';
for (k in a) {
result += (k + ' = ' + a[k] + '\n');
}
alert(result);
return result;
}

It is meant to be called like:

var a = 'test';
var b = 123;
var c = true;

alertt({
a:a,
b:b,
c:c
});

To explain these line if they aren't immediately clear: the function
takes an object with the keys as the names of the variables and the
values as the values of the variables. So the "a:a" sets the key on
the left of the colon to "a" and the value on the right of the colon
to the value of "a".

I don't think a try...catch helps in the version I wrote because when
I've tested it the Reference Error occurs on the line that calls the
function. I think the trade off is that you could use the try...catch
in the original string/eval function but it has that problem with only
working on globals whereas I'm not sure how I'd use try...catch in the
function I wrote, but it should be able to understand any variables
that are available to the level of scope from which it is called.

Hope another take on the function provides some insight.

--
Dan Evans
Sep 3 '08 #6
I think I should add that this function is probably never going to be
as useful as firebug (an extension for the firefox browser,
http://getfirebug.com/). The reasons for this are that alert() halts
execution and so can be bad for testing things like animations where
you might want to output some values every iteration and you want to
iterate multiple times per second. (If you did want to halt execution
with firebug it provides a way to insert breakpoints and step through
execution.) I think there is a firebug lite which will provide some of
this functionality in browsers beside firefox. Finally the biggest
reason I see for using a more robust tool is that you may need to
recursively read the keys and values of nested objects. Something
like:

var foo = {
bar : {
baz : 'qux'
}
}

So far our alertt functions won't allow for nice inspection of an
object like that.

--
Dan Evans
Sep 3 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: dterrors | last post by:
Will php 6 do strong typing and/or namespaces? I was shocked to find out today that there are some people who actually argue that weak typing is somehow better. I didn't even know there was a...
12
by: Jason Tesser | last post by:
I work for at a college where I am one of 2 full-time developers and we are looking to program a new software package fro the campus. This is a huge project as it will include everything from...
94
by: Gabriel Zachmann | last post by:
Is it correct to say that strong/weak typing does not make a difference if one does not use any pointers (or adress-taking operator)? More concretely, I am thinking particularly of Python vs C++....
49
by: bearophileHUGS | last post by:
Adding Optional Static Typing to Python looks like a quite complex thing, but useful too: http://www.artima.com/weblogs/viewpost.jsp?thread=85551 I have just a couple of notes: Boo...
5
by: lk | last post by:
Hi there, is it possible to avoid that a user could enter a text directly in a combobox? The user should only select a text from the list of the combobox without typing anything. Thanks
5
by: Novice Computer User | last post by:
Hi. Can somebody PLEASE help. Here is a .php script. Right now, the minimum amount of time (i.e. duration) allowed is 1 month. However, I want to be able to reduce the minimum amount of time to...
11
by: Martin Jørgensen | last post by:
Hi, - - - - - - - - - - - - - - - #include <iostream> #include <string> #include <map> using namespace std; int main() {
26
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I...
0
by: Mike Kansky | last post by:
I have a task bar application with Outlook-like alerts that are displayed in the form. Every time the alert is displayed the alert FORM gets focus, which is very inconvinient if a user was in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.