473,386 Members | 1,705 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,386 software developers and data experts.

Why does this work?

Normally I ask why things DON'T work!!

However, according to what I have read at
http://subsimple.com/bookmarklets/rules.asp the bookmarklet:

javascript:q = prompt("Enter destination or leave blank for main maps
page.",
"");location='http://maps.google.co.uk/'+((q.length)?'maps?f=d&hl=en&saddr=po4+8ej&daddr= '+escape(q.replace(/
/g, "+"))+'&om=1':'')

should cause a problem because the assignment of the prompt to q is not
voided and the script is not encapsulated in a function.

So why am I not getting an error? Is it because the location assignment is
taking precedent?

TIA
Regards
Paul
May 23 '06 #1
6 1332
VK

Paul Lautman wrote:
Normally I ask why things DON'T work!!

However, according to what I have read at
http://subsimple.com/bookmarklets/rules.asp the bookmarklet:

javascript:q = prompt("Enter destination or leave blank for main maps
page.",
"");location='http://maps.google.co.uk/'+((q.length)?'maps?f=d&hl=en&saddr=po4+8ej&daddr= '+escape(q.replace(/
/g, "+"))+'&om=1':'')

should cause a problem because the assignment of the prompt to q is not
voided and the script is not encapsulated in a function.

So why am I not getting an error? Is it because the location assignment is
taking precedent?


Well, strictly and officially javascript: pseudo-protocol has nothing
to do with bookmarklets: it is the closest relative of document.write
(used after document load). Yet Brendan Eich foresaw some possible
creative usage of this protocol including ill-fated psi-links and
bookmarklets see more at
<http://en.wikipedia.org/wiki/Bookmarklet#History>

That's an introductory blah-blah :-) as a comment on that funny
statement in the page you linked:
<q>If a function or an assignment returns a value, which you do not
catch, the bookmarklet will redirect to a new page that shows the
returned value. Silly, I know, but that's what it does. You can
intercept the return value by encapsulating the relevant statements
with void(...).</q>

Indeed, "silly" to see something working in the intended way instead of
the hacked way :-)

On your direct question:
As user are lasy and refusing to read any specs by their very nature
:-), "extended" javascript: protocol usage is as simplified as
possible. As long as you don't return any value back explicetly (via
return), it presumes that you don't want to use it as a javascript
source for the next page.

In the posted bookmarklet you don't return anything explicetly, so you
are fine. Yet I would suggest to use void operator in your own
bookmarklets - just in case.

May 23 '06 #2
VK wrote:
Paul Lautman wrote:
Normally I ask why things DON'T work!!

However, according to what I have read at
http://subsimple.com/bookmarklets/rules.asp the bookmarklet:

javascript:q = prompt("Enter destination or leave blank for main maps
page.",
"");location='http://maps.google.co.uk/'+((q.length)?'maps?f=d&hl=en&saddr=po4+8ej&daddr= '+escape(q.replace(/
/g, "+"))+'&om=1':'')

should cause a problem because the assignment of the prompt to q is
not voided and the script is not encapsulated in a function.

So why am I not getting an error? Is it because the location
assignment is taking precedent?


Well, strictly and officially javascript: pseudo-protocol has nothing
to do with bookmarklets: it is the closest relative of document.write
(used after document load). Yet Brendan Eich foresaw some possible
creative usage of this protocol including ill-fated psi-links and
bookmarklets see more at
<http://en.wikipedia.org/wiki/Bookmarklet#History>

That's an introductory blah-blah :-) as a comment on that funny
statement in the page you linked:
<q>If a function or an assignment returns a value, which you do not
catch, the bookmarklet will redirect to a new page that shows the
returned value. Silly, I know, but that's what it does. You can
intercept the return value by encapsulating the relevant statements
with void(...).</q>

Indeed, "silly" to see something working in the intended way instead
of the hacked way :-)

On your direct question:
As user are lasy and refusing to read any specs by their very nature
:-), "extended" javascript: protocol usage is as simplified as
possible. As long as you don't return any value back explicetly (via
return), it presumes that you don't want to use it as a javascript
source for the next page.

In the posted bookmarklet you don't return anything explicetly, so you
are fine. Yet I would suggest to use void operator in your own
bookmarklets - just in case.


Thank you VK
May 23 '06 #3
"VK" <sc**********@yahoo.com> writes:
Paul Lautman wrote:
So why am I not getting an error? Is it because the location assignment is
taking precedent?

Yes. I wouldn't depend on it working in all browsers.
As user are lasy and refusing to read any specs by their very nature
:-), "extended" javascript: protocol usage is as simplified as
possible. As long as you don't return any value back explicetly (via
return), it presumes that you don't want to use it as a javascript
source for the next page.
That's not how browsers work.

Change the assignment to, e.g, "locationx" instead of "location", and
see that the page is indeed replaced by the string value of that
assignment, even without a return value.

When following a "javascript:"-link, the browser evaluates the
following statements as if it was the argument of the eval function,
i.e., the result is the value of the last evaluated expression
statement or the value returned by a return statement.

If that value is "undefined", no replacement happens. Otherwise the
result is converted to a string (as by the String function) and a new
page is created with that string value as its source code.

In this case, evaluation of the javascript code had a side effect of
loading another page. In the browsers you have tested, that
replacement was the one to take effect.
In the posted bookmarklet you don't return anything explicetly, so you
are fine. Yet I would suggest to use void operator in your own
bookmarklets - just in case.


I would suggest learning when and how the void operator is applicable
and suitabel. Don't insert it just because you think you have to.
Insert it because you know! Otherwise it's just programming by
coincidence ("it works, but I don't know why", which often means
that there are cases where it fails to work, and you don't know
where to look for them).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
May 24 '06 #4
VK

Lasse Reichstein Nielsen wrote:
I would suggest learning when and how the void operator is applicable
and suitabel. Don't insert it just because you think you have to.


Very true.
In this case (psi-links and bookmarklets) it was suggested and
illustrated by Brendan Eich, so I see it as a valid use (which is it
actually). There is always a possibility of course that Brendan Eich
doesn't know how to program in JavaScript properly - sometimes even
original inventors cannot deal with evolutionated versions of their
inventions. I don't see it as the case here though. :-)

May 24 '06 #5
Paul Lautman wrote:
However, according to what I have read at
http://subsimple.com/bookmarklets/rules.asp the bookmarklet:

javascript:q = prompt("Enter destination or leave blank for main maps
page.",
"");location='http://maps.google.co.uk/'+((q.length)?'maps?f=d&hl=en&saddr=po4+8ej&daddr= '+escape(q.replace(/
/g, "+"))+'&om=1':'')

should cause a problem because the assignment of the prompt to q is not
voided and the script is not encapsulated in a function.

So why am I not getting an error? Is it because the location assignment is
taking precedent?


First of all, ignore VK's fiction.

Second, you do not get an error because the source code is syntactically
correct.

Third, the common implementation of the proprietary `javascript:'
pseudo-protocol is that only the result of the last evaluation is used to
create a HTML document with the evaluated value as content. Since the last
statement is an assignment to the host-defined `location' property of the
Global Object, as a peculiarity of HTML UA's Application Object Model
(AOM), the Global Object and the execution context the script code runs in
ceases to exist (and is recreated later). Hence no temporary document is
generated from that assigned value.

Simply remove the `location=' assignment (or change it to `locationx' or any
other property that does not affect navigation, as Lasse suggested) and
observe that the value assigned to `q' (or `locationx') is used to generate
that temporary document.
PointedEars
--
The German psychs, the German authorities, the German secret service agents
are [...] fanatics, they are insane and known of persecuting innocent people
and Scientologists. -- "The only real Barbara Schwarz", dsw.scientology,
<16**************************@posting.google.com >
May 25 '06 #6
VK

Lasse Reichstein Nielsen wrote:
Change the assignment to, e.g, "locationx" instead of "location", and
see that the page is indeed replaced by the string value of that
assignment, even without a return value.

When following a "javascript:"-link, the browser evaluates the
following statements as if it was the argument of the eval function,
i.e., the result is the value of the last evaluated expression
statement or the value returned by a return statement.


That's a nice observation, good to know. I guess I always was just too
accurate with using javascript: protocol to notice that :-)

In the original introduction of javascript: Brendan Eich just says:
<q>If the value of the expression following a javascript: URL prefix
evaluates to undefined, no new document is loaded. If the expression
evaluates to a defined type, the value is converted to a string that
specifies the source of the document to load.</a>

But the behavior you pointed to is consistent at least down to NN4.5 -
and it doesn't really anyhow contradict to the quote above (just could
be clearer in the mechanics, but this can be said about way many too
many specs :-)

But now it is also crearer why Mr. Eich was suggesting to use void()
wrapper for the code: exactly for the situations when you can still get
a return value without returning anything explicetly - just because of
say script error in the code to evaluate.

May 25 '06 #7

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

Similar topics

7
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
3
by: Julian | last post by:
Hi I am trying to update a date field in my table but some how this simple code does not work, I know the select work because if I write the fields, it will show the data from the table but why...
5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
22
by: Robert Bralic | last post by:
CAN anybody tell me any address where I can download some small(1000-2000) lines C++ proghram source. Or send me ,a small(1000-2000) lines C++ program source that I can compille with gpp under...
12
by: Frank Hauptlorenz | last post by:
Hello Out there! I have a DB2 V7.2 Database (Fix11) on Win 2000 Professional. It was before a NT 4 based Domain - now it is a Win 2000 Domain. The database server is a domain member. Now...
0
by: Jarod_24 | last post by:
How does tabindex work in ASP .net pages I dosen't seem to work quite like in regular forms. and there isn't any TabStop property either. 1 .How do you prevent a control form beign "tabbed"....
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
14
by: webEater | last post by:
I have a problem, it's not browser specific, and I don't get a solution. I have an (X)HTML document, I show you a part of it: .... <!--<div class="pad">--> <div id="eventImages"><img src=""...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.