Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

JSON without AJAX

Question posted by: VK (Guest) on December 22nd, 2005 11:15 AM
Mission statement:

A mechanics to get text stream into browser from any Web location
without reloading the current page.

1) This mechanics has to support *at the very least* IE 5.5 and higher
and Firefox 1.5 and higher: but it may be completely different from one
browser to another. It is important only to be able to build an
uniformed interface atop of it.

2) This mechanics has to exploit some core features of modern browsers
so it would not be possible to lock it without stopping the browser to
be a browser (like visited links sniffing).

Com'on guys: let's hack sh** out of them! :-)

My initial donation:
1)
<link
id = "JSON"
title="JSON"
rel="alternate appendix"
type="text/plain"
href="data.txt"
charset="iso-8859-1"
hreflang="en-US">

Not even a hack, but seems to way to get the data.txt content

2)
<link
id = "JSON"
title="JSON"
rel="alternate stylesheet"
type="text/css"
href="data.css"
charset="iso-8859-1"
hreflang="en-US">

That would be nearly ideal as style rule declaration is almost
identical to JSON object syntaxes. But unknown attributes are ignored
so:
..data {
key1:value1;
key2:value2;
key3:value3;
}
will result into empty parenthesis
..data {}

Any way to stop it?

3)
IE-only "download" behavior
Not explored plus seems too easy to "upgrade security" if Microsoft
decides to. Still decided to mention.


P.S. If you find some really breathtaking solution conformant to the
requirement 2) above you may want to try to copyright it first before
posting in a public forum. Or you may don't - but needs to be
mentioned.

RobG's Avatar
RobG
Guest
n/a Posts
December 22nd, 2005
01:45 PM
#2

Re: JSON without AJAX
VK wrote:[color=blue]
> Mission statement:
>
> A mechanics to get text stream into browser from any Web location
> without reloading the current page.
>
> 1) This mechanics has to support *at the very least* IE 5.5 and higher
> and Firefox 1.5 and higher: but it may be completely different from one
> browser to another. It is important only to be able to build an
> uniformed interface atop of it.
>
> 2) This mechanics has to exploit some core features of modern browsers
> so it would not be possible to lock it without stopping the browser to
> be a browser (like visited links sniffing).
>
> Com'on guys: let's hack sh** out of them! :-)[/color]

No hack required:

<script type="text/javascript" src="json.data"></script>


You can even dynamically load the script files using DOM to add the
script elements.


[...]
[color=blue]
>
> P.S. If you find some really breathtaking solution conformant to the
> requirement 2) above you may want to try to copyright it first before
> posting in a public forum.[/color]

No need to 'try to copyright it'. Original work is copyright the
moment it is fixed in some medium (in most jurisdictions that observe
copyright).

[color=blue]
> Or you may don't - but needs to be
> mentioned.[/color]

No, it doesn't.


--
Rob

VK's Avatar
VK
Guest
n/a Posts
December 22nd, 2005
02:05 PM
#3

Re: JSON without AJAX

RobG wrote:[color=blue]
> No hack required:
> <script type="text/javascript" src="json.data"></script>
> You can even dynamically load the script files using DOM to add the
> script elements.[/color]

That was the most obvious one, already discussed in this group.
Drawbacks:
1) Interpreter will try to parse received data right on arrival. For
serialized objects it will create unnecessary overheat plus potential
error stream.
Does
scriptObject.defer = true
help in that?

2) IE supports onreadystatechange for scriptObject.
readyState comes in traditional IE's string notation:
"uninitialized"
"loading"
"loaded"
"interactive"
"complete"
(instead of 0 -4, that was IE's partial compromise for AJAX users).

That would be easy to fix but up to date no other rivals seems to
support onreadystatechange except for XMLHttpRequest (?)

So how to get state update in a nice and reliable way?
[color=blue]
> No need to 'try to copyright it'. Original work is copyright the
> moment it is fixed in some medium (in most jurisdictions that observe
> copyright).[/color]

Read the already discussed GIF story. At least in the USA if you post
some information in a public media and do not state any copyrights in
such publication, it is assumed that you forward it to the public
domain. Still reversable but easier to say right away "This work is
mine and I want to copyright it as soon as I can". Why make things more
difficult as they should be? ;-)


VK's Avatar
VK
Guest
n/a Posts
December 22nd, 2005
02:05 PM
#4

Re: JSON without AJAX
> easier to say right away "This work is[color=blue]
> mine and I want to copyright it as soon as I can".[/color]

As you may notice * I * personally do not say it anywhere.
But I cannot act for everyone else.


bwucke@gmail.com's Avatar
bwucke@gmail.com
Guest
n/a Posts
December 22nd, 2005
02:15 PM
#5

Re: JSON without AJAX
On related note, any way to access/process the data (binary) of an
image? (<img src> or Image() ?)


re: copyright. By law you own copyright to anything you publish unless
you state otherwise. Thing is, you need some kind of proof you're the
original author. Anyone can publish your work and (wrongly) claim
copyright on it, and unless you can prove you created it before they
did, you can do nothing about it even if lawfully it belongs to you.
Doesn't work on patenting, ideas enter public domain and become
unpatentable when published - so nobody can copy your code verbatim
legally but they can use the same ideas.


VK's Avatar
VK
Guest
n/a Posts
December 22nd, 2005
02:25 PM
#6

Re: JSON without AJAX

Join Bytes! wrote:[color=blue]
> On related note, any way to access/process the data (binary) of an
> image? (<img src> or Image() ?)[/color]
[color=blue]
>From the disk or from the web?[/color]

On Firefox and Netscape you can use undocumented but perfectly
functionning btoa() and atob() methods to convert data to base64 and
back for Web transmission.
IE doesn'r have such methods so they need to be emulated by custom
JavaScript program. Due to some buffer limitations any solution will
get dead slow or even crashes for files approx. > 100Kb

Explicit binary transmission (or reading binary files from disk) is not
allowed in IE: you're getting only the file header.

Noop :-(
[color=blue]
> re: copyright. By law you own copyright to anything you publish unless
> you state otherwise. Thing is, you need some kind of proof you're the
> original author. Anyone can publish your work and (wrongly) claim
> copyright on it, and unless you can prove you created it before they
> did, you can do nothing about it even if lawfully it belongs to you.
> Doesn't work on patenting, ideas enter public domain and become
> unpatentable when published - so nobody can copy your code verbatim
> legally but they can use the same ideas.[/color]

Good to know to everyone, thanks.


VK's Avatar
VK
Guest
n/a Posts
December 22nd, 2005
02:45 PM
#7

Re: JSON without AJAX
Join Bytes! wrote:[color=blue]
> On related note, any way to access/process the data (binary) of an
> image? (<img src> or Image() ?)[/color]

Thinking over:

What DOM objects ever had or have or will have loadable .location .src
or .href or similar property?

Off my head:
window
frame
document
iframe
link (style)
script
object (HTML) ?
image

Is it all?


bwucke@gmail.com's Avatar
bwucke@gmail.com
Guest
n/a Posts
December 22nd, 2005
05:25 PM
#8

Re: JSON without AJAX
> Is it all?
A
APPLET
AREA
BASE
INPUT type=image
META (sometimes contains URLs...)
everything that takes "longdesc".


Thomas 'PointedEars' Lahn's Avatar
Thomas 'PointedEars' Lahn
Guest
n/a Posts
December 22nd, 2005
06:05 PM
#9

Re: JSON without AJAX
Join Bytes! wrote:
[color=blue][color=green]
>> Is it all?[/color]
> A
> APPLET
> AREA
> BASE
> INPUT type=image
> META (sometimes contains URLs...)
> everything that takes "longdesc".[/color]

He referred to "DOM objects ever had or have or will have
_loadable_ .location .src or .href or similar property",
note the Subject.

If you would have quoted what you are referring to, you
would have known.

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>
<URL:http://www.safalra.com/special/googlegroupsreply/>


PointedEars

VK's Avatar
VK
Guest
n/a Posts
December 22nd, 2005
06:15 PM
#10

Re: JSON without AJAX

Thomas 'PointedEars' Lahn wrote:[color=blue]
> Join Bytes! wrote:
>[color=green][color=darkred]
> >> Is it all?[/color]
> > A
> > APPLET
> > AREA
> > BASE
> > INPUT type=image
> > META (sometimes contains URLs...)
> > everything that takes "longdesc".[/color][/color]


INPUT type=image
At least one hit to the piggybank. But I'm affraid by usability same as
Image - see higher in this thread.
I had no idea about "longdesc". But like META or A - just passive URI
linking.

Here is the property dump of SCRIPT object in Firefox 1.5 I'm wondering
if any of properties could be checked for presence / availability
during the script loading to emulate onreadystatechange functionality.
Not to say I cannot do it myself - but someone may have an immediate
answer or comment.

nodeName = SCRIPT
nodeValue = null
nodeType = 1
parentNode = [object HTMLHeadElement]
childNodes = [object NodeList]
firstChild = [object Text]
lastChild = [object Text]
previousSibling = [object Text]
nextSibling = null
attributes = [object NamedNodeMap]
ownerDocument = [object HTMLDocument]
insertBefore = function insertBefore() {
[native code]
}
replaceChild = function replaceChild() {
[native code]
}
removeChild = function removeChild() {
[native code]
}
appendChild = function appendChild() {
[native code]
}
hasChildNodes = function hasChildNodes() {
[native code]
}
cloneNode = function cloneNode() {
[native code]
}
normalize = function normalize() {
[native code]
}
isSupported = function isSupported() {
[native code]
}
namespaceURI = null
prefix = null
localName = SCRIPT
hasAttributes = function hasAttributes() {
[native code]
}
tagName = SCRIPT
getAttribute = function getAttribute() {
[native code]
}
setAttribute = function setAttribute() {
[native code]
}
removeAttribute = function removeAttribute() {
[native code]
}
getAttributeNode = function getAttributeNode() {
[native code]
}
setAttributeNode = function setAttributeNode() {
[native code]
}
removeAttributeNode = function removeAttributeNode() {
[native code]
}
getElementsByTagName = function getElementsByTagName() {
[native code]
}
getAttributeNS = function getAttributeNS() {
[native code]
}
setAttributeNS = function setAttributeNS() {
[native code]
}
removeAttributeNS = function removeAttributeNS() {
[native code]
}
getAttributeNodeNS = function getAttributeNodeNS() {
[native code]
}
setAttributeNodeNS = function setAttributeNodeNS() {
[native code]
}
getElementsByTagNameNS = function getElementsByTagNameNS() {
[native code]
}
hasAttribute = function hasAttribute() {
[native code]
}
hasAttributeNS = function hasAttributeNS() {
[native code]
}
id =
title =
lang =
dir =
className =
text =
function init() {
var out = document.forms[0].out;
var js = document.getElementsByTagName('SCRIPT')[0];
for (i in js) {out.value+= i+' = '+js[i]+'\n';}
}

htmlFor =
event =
charset =
defer = false
src =
type = text/javascript
ELEMENT_NODE = 1
ATTRIBUTE_NODE = 2
TEXT_NODE = 3
CDATA_SECTION_NODE = 4
ENTITY_REFERENCE_NODE = 5
ENTITY_NODE = 6
PROCESSING_INSTRUCTION_NODE = 7
COMMENT_NODE = 8
DOCUMENT_NODE = 9
DOCUMENT_TYPE_NODE = 10
DOCUMENT_FRAGMENT_NODE = 11
NOTATION_NODE = 12
offsetTop = 0
offsetLeft = 0
offsetWidth = 0
offsetHeight = 0
offsetParent = null
innerHTML =
function init() {
var out = document.forms[0].out;
var js = document.getElementsByTagName('SCRIPT')[0];
for (i in js) {out.value+= i+' = '+js[i]+'\n';}
}

scrollTop = 0
scrollLeft = 0
scrollHeight = 0
scrollWidth = 0
clientHeight = 0
clientWidth = 0
tabIndex = -1
blur = function blur() {
[native code]
}
focus = function focus() {
[native code]
}
style = [object CSSStyleDeclaration]
removeEventListener = function removeEventListener() {
[native code]
}
dispatchEvent = function dispatchEvent() {
[native code]
}
baseURI = file:///D:/JS/TMP1135277704.htm
compareDocumentPosition = function compareDocumentPosition() {
[native code]
}
textContent =
function init() {
var out = document.forms[0].out;
var js = document.getElementsByTagName('SCRIPT')[0];
for (i in js) {out.value+= i+' = '+js[i]+'\n';}
}

isSameNode = function isSameNode() {
[native code]
}
lookupPrefix = function lookupPrefix() {
[native code]
}
isDefaultNamespace = function isDefaultNamespace() {
[native code]
}
lookupNamespaceURI = function lookupNamespaceURI() {
[native code]
}
isEqualNode = function isEqualNode() {
[native code]
}
getFeature = function getFeature() {
[native code]
}
setUserData = function setUserData() {
[native code]
}
getUserData = function getUserData() {
[native code]
}
DOCUMENT_POSITION_DISCONNECTED = 1
DOCUMENT_POSITION_PRECEDING = 2
DOCUMENT_POSITION_FOLLOWING = 4
DOCUMENT_POSITION_CONTAINS = 8
DOCUMENT_POSITION_CONTAINED_BY = 16
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 32


Thomas 'PointedEars' Lahn's Avatar
Thomas 'PointedEars' Lahn
Guest
n/a Posts
December 22nd, 2005
06:45 PM
#11

Re: JSON without AJAX
VK wrote:
[color=blue]
> Thomas 'PointedEars' Lahn wrote:[color=green]
>> Join Bytes! wrote:[color=darkred]
>> >> Is it all?
>> > A
>> > APPLET
>> > AREA
>> > BASE
>> > INPUT type=image
>> > META (sometimes contains URLs...)
>> > everything that takes "longdesc".[/color][/color]
>
> INPUT type=image
> At least one hit to the piggybank.[/color]

And I missed "APPLET". Adding OBJECT.
[color=blue]
> But I'm affraid by usability same as Image -[/color]

Full ACK
[color=blue]
> see higher in this thread.[/color]

"Higher"? :)
[color=blue]
> Here is the property dump of SCRIPT object in Firefox 1.5 I'm wondering
> if any of properties could be checked for presence / availability
> during the script loading to emulate onreadystatechange functionality.
> Not to say I cannot do it myself - but someone may have an immediate
> answer or comment.[/color]

It is not possible to determine if a script was loaded by inspecting the
properties of a HTMLScriptElement object. What would be possible is to
determine if a certain variable was declared and holding a value different
from `undefined'. That said, "loading scripts" after the document was
loaded is still unreliable.


PointedEars

Randy Webb's Avatar
Randy Webb
Guest
n/a Posts
December 22nd, 2005
09:25 PM
#12

Re: JSON without AJAX
Thomas 'PointedEars' Lahn said the following on 12/22/2005 2:32 PM:

[color=blue]
>That said, "loading scripts" after the document was loaded is still unreliable.[/color]

No more unreliable than trying to load them when the page is loading.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Thomas 'PointedEars' Lahn's Avatar
Thomas 'PointedEars' Lahn
Guest
n/a Posts
December 22nd, 2005
09:45 PM
#13

Re: JSON without AJAX
Randy Webb wrote:
[color=blue]
> Thomas 'PointedEars' Lahn said the following on 12/22/2005 2:32 PM:[color=green]
>> That said, "loading scripts" after the document was loaded is still
>> unreliable.[/color]
>
> No more unreliable than trying to load them when the page is loading.[/color]

Nonsense.


PointedEars

VK's Avatar
VK
Guest
n/a Posts
December 22nd, 2005
09:45 PM
#14

Re: JSON without AJAX

Randy Webb wrote:[color=blue]
> Thomas 'PointedEars' Lahn said the following on 12/22/2005 2:32 PM:
>
>[color=green]
> >That said, "loading scripts" after the document was loaded is still unreliable.[/color]
>
> No more unreliable than trying to load them when the page is loading.
>[/color]

if (scriptObject.readyState == 'complete')
in IE works just fine and reliable - at least seems so, I did not check
it under all circumstances.

It is a shame that others did not implement anything so convenient and
obvious - except for XMLHttpRequest - they must be thinking that after
such glorious achievement they can take a rest for another year or two.
:-)

The previous script properties dump was irrelevant (sorry for that) as
it was for inline script. Below the properties dump for external script
for Firefox 1.5 Win98 SE

So much of useless crap they put in it - and nothing really useful.
That's still would be the best to check the state w/o any in-script
helpers, to keep the data out of implementation.

nodeName = SCRIPT
nodeValue = null
nodeType = 1
parentNode = [object HTMLHeadElement]
childNodes = [object NodeList]
firstChild = null
lastChild = null
previousSibling = [object Text]
nextSibling = null
attributes = [object NamedNodeMap]
ownerDocument = [object HTMLDocument]
insertBefore = function insertBefore() {
[native code]
}
replaceChild = function replaceChild() {
[native code]
}
removeChild = function removeChild() {
[native code]
}
appendChild = function appendChild() {
[native code]
}
hasChildNodes = function hasChildNodes() {
[native code]
}
cloneNode = function cloneNode() {
[native code]
}
normalize = function normalize() {
[native code]
}
isSupported = function isSupported() {
[native code]
}
namespaceURI = null
prefix = null
localName = SCRIPT
hasAttributes = function hasAttributes() {
[native code]
}
tagName = SCRIPT
getAttribute = function getAttribute() {
[native code]
}
setAttribute = function setAttribute() {
[native code]
}
removeAttribute = function removeAttribute() {
[native code]
}
getAttributeNode = function getAttributeNode() {
[native code]
}
setAttributeNode = function setAttributeNode() {
[native code]
}
removeAttributeNode = function removeAttributeNode() {
[native code]
}
getElementsByTagName = function getElementsByTagName() {
[native code]
}
getAttributeNS = function getAttributeNS() {
[native code]
}
setAttributeNS = function setAttributeNS() {
[native code]
}
removeAttributeNS = function removeAttributeNS() {
[native code]
}
getAttributeNodeNS = function getAttributeNodeNS() {
[native code]
}
setAttributeNodeNS = function setAttributeNodeNS() {
[native code]
}
getElementsByTagNameNS = function getElementsByTagNameNS() {
[native code]
}
hasAttribute = function hasAttribute() {
[native code]
}
hasAttributeNS = function hasAttributeNS() {
[native code]
}
id =
title =
lang =
dir =
className =
text =
htmlFor =
event =
charset =
defer = false
src = file:///D:/JS/external.js
type = text/javascript
ELEMENT_NODE = 1
ATTRIBUTE_NODE = 2
TEXT_NODE = 3
CDATA_SECTION_NODE = 4
ENTITY_REFERENCE_NODE = 5
ENTITY_NODE = 6
PROCESSING_INSTRUCTION_NODE = 7
COMMENT_NODE = 8
DOCUMENT_NODE = 9
DOCUMENT_TYPE_NODE = 10
DOCUMENT_FRAGMENT_NODE = 11
NOTATION_NODE = 12
offsetTop = 0
offsetLeft = 0
offsetWidth = 0
offsetHeight = 0
offsetParent = null
innerHTML =
scrollTop = 0
scrollLeft = 0
scrollHeight = 0
scrollWidth = 0
clientHeight = 0
clientWidth = 0
tabIndex = -1
blur = function blur() {
[native code]
}
focus = function focus() {
[native code]
}
style = [object CSSStyleDeclaration]
removeEventListener = function removeEventListener() {
[native code]
}
dispatchEvent = function dispatchEvent() {
[native code]
}
baseURI = file:///D:/JS/TMP1135285699.htm
compareDocumentPosition = function compareDocumentPosition() {
[native code]
}
textContent =
isSameNode = function isSameNode() {
[native code]
}
lookupPrefix = function lookupPrefix() {
[native code]
}
isDefaultNamespace = function isDefaultNamespace() {
[native code]
}
lookupNamespaceURI = function lookupNamespaceURI() {
[native code]
}
isEqualNode = function isEqualNode() {
[native code]
}
getFeature = function getFeature() {
[native code]
}
setUserData = function setUserData() {
[native code]
}
getUserData = function getUserData() {
[native code]
}
DOCUMENT_POSITION_DISCONNECTED = 1
DOCUMENT_POSITION_PRECEDING = 2
DOCUMENT_POSITION_FOLLOWING = 4
DOCUMENT_POSITION_CONTAINS = 8
DOCUMENT_POSITION_CONTAINED_BY = 16
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 32


Thomas 'PointedEars' Lahn's Avatar
Thomas 'PointedEars' Lahn
Guest
n/a Posts
December 22nd, 2005
11:35 PM
#15

Re: JSON without AJAX
VK wrote:
[color=blue]
> Randy Webb wrote:[color=green]
>> Thomas 'PointedEars' Lahn said the following on 12/22/2005 2:32 PM:[color=darkred]
>> > That said, "loading scripts" after the document was loaded is still
>> > unreliable.[/color]
>> No more unreliable than trying to load them when the page is loading.[/color]
>
> if (scriptObject.readyState == 'complete')
> in IE works just fine and reliable - at least seems so, I did not check
> it under all circumstances.[/color]

"Reliable" means it works under _all_ circumstances.
[color=blue]
> It is a shame that others did not implement anything so convenient and
> obvious [...][/color]

It is not. They just did not expect that people would try to add `script'
elements in order to load and execute scripts after the document was
loaded. ISTM that it does what it does if it does is merely coincidental,
which is why I am strongly recommending against it.
[color=blue]
> The previous script properties dump was irrelevant (sorry for that) as
> it was for inline script. Below the properties dump for external script
> for Firefox 1.5 Win98 SE[/color]

It is still irrelevant, stop posting such.


PointedEars

Randy Webb's Avatar
Randy Webb
Guest
n/a Posts
December 23rd, 2005
03:05 AM
#16

Re: JSON without AJAX
Thomas 'PointedHead' Lahn babbled the following incoherently in
comp.lang.javascript:[color=blue]
> Randy Webb wrote:[color=green]
>>Thomas 'PointedEars' Lahn said the following on 12/22/2005 2:32 PM:[color=darkred]
>>>That said, "loading scripts" after the document was loaded is still
>>>unreliable.[/color]
>>No more unreliable than trying to load them when the page is loading.[/color]
> Nonsense.[/color]

I can load .js files dynamically just as reliably as I can load them
statically. Either something is reliable and always works, or, it is
unreliable and doesn't always work. Since external files can never
"always be loaded" then it is unreliable. As is loading them
dynamically. That makes them both unreliable. And unreliable is not
relative. If two things are both unreliable, then they are equally
unreliable.

In the future, it might help you if you would endeavor to understand the
language I am using and the meanings of what I post. It would keep you
from replying as much and keep me from correcting you so much.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Randy Webb's Avatar
Randy Webb
Guest
n/a Posts
December 23rd, 2005
03:05 AM
#17

Re: JSON without AJAX
Thomas 'PointedEars' Lahn said the following on 12/22/2005 7:20 PM:[color=blue]
> VK wrote:[color=green]
>>Randy Webb wrote:[color=darkred]
>>>Thomas 'PointedEars' Lahn said the following on 12/22/2005 2:32 PM:
>>>>That said, "loading scripts" after the document was loaded is still
>>>>unreliable.
>>>No more unreliable than trying to load them when the page is loading.[/color]
>>if (scriptObject.readyState == 'complete')
>>in IE works just fine and reliable - at least seems so, I did not check
>>it under all circumstances.[/color]
> "Reliable" means it works under _all_ circumstances.[/color]

And loading .js files is _never_ reliable as there will be circumstances
where it won't load. Remember that, it will prove beneficial to you.
[color=blue][color=green]
>>It is a shame that others did not implement anything so convenient and
>>obvious [...][/color]
> It is not.[/color]

What is not? The convenience of readyState? The obviousness of
readyState? Or that it is a shame? It is a shame indeed that no other
browser gives you a method to tell when a script has loaded.
[color=blue]
> They just did not expect that people would try to add `script'
> elements in order to load and execute scripts after the document was
> loaded.[/color]

And how do you know what the programmers at MS "expected"? Or is that
more of your holier-than-thou-but-worthless thought process?
[color=blue]
> ISTM that it does what it does if it does is merely coincidental,
> which is why I am strongly recommending against it.[/color]

Recommending against what? Using readyState or loading .js files
dynamically?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

bwucke@gmail.com's Avatar
bwucke@gmail.com
Guest
n/a Posts
December 23rd, 2005
06:15 AM
#18

Re: JSON without AJAX

Randy Webb napisal(a):[color=blue]
> As is loading them
> dynamically. That makes them both unreliable. And unreliable is not
> relative. If two things are both unreliable, then they are equally
> unreliable.
>[/color]

Internet is unreliable.


Jasen Betts's Avatar
Jasen Betts
Guest
n/a Posts
December 23rd, 2005
08:25 AM
#19

Re: JSON without AJAX
On 2005-12-22, VK <schools_ring@yahoo.com> wrote:[color=blue]
> Join Bytes! wrote:[color=green]
>> On related note, any way to access/process the data (binary) of an
>> image? (<img src> or Image() ?)[/color]
>
> Thinking over:
>
> What DOM objects ever had or have or will have loadable .location .src
> or .href or similar property?
>
> Off my head:
> window
> frame
> document
> iframe
> link (style)
> script
> object (HTML) ?
> image
>
> Is it all?[/color]

<link rel="shortcut icon" > (and variants)
<style> (I think ICBW)



--

Bye.
Jasen

VK's Avatar
VK
Guest
n/a Posts
December 23rd, 2005
09:35 AM
#20

Re: JSON without AJAX

Thomas 'PointedEars' Lahn wrote:[color=blue]
> "Reliable" means it works under _all_ circumstances.[/color]

Then let's define exactly what "reliable" would mean in this case and
what circumstances should be expected.

(pseudo-code)

Variant 1 - hardcoded kill
[remove existing node] scriptObject
[add new node] scriptObject
[assign] scriptObject.src = newURL

Does removing the node devalidate the current script context of
scriptObject - so it would be free for garbage collection?

What moment such devalidation occurs (if it does) - immediately upon
removing the node? somewhere later?

What moment we can try to access new scriptObject? When the
text/javascript input stream is closed? When interpreter finished some
tuneup job somether after? Or even while text/javascript input stream
is still open?

If newURL doesn't exists or is not reachable: do we have any formal
signs to see the trouble?

How scriptObject.defer = true affects (if it does) on anything above?

(defer is now supported by IE, FF, Opera - not sure about Safari 2.x.
Safari 1.x and Konqueror in any shall perform form are out of the loop
as hopeless cases)


Variant 2 - soft replacement
scriptObject.src = newURL1
...
scriptObject.src = newURL2
...
etc.

Same questions but getting even more tricky.
I doubt very much that there are any written standards for it - but I
can be crossly mistaken.
If I'm write though then the answers have to be found from experiments.
The most stable (wide spread) behavior should be announced as the
standard then and submitted to W3C as standard proposal - so future
browser developers could refer to it. Will it be accepted by W3C or not
is not important - there are plenty of standards which are sitting as
proposals for years.

Erratic behaviors in particular browsers/versions should be found and
fixed by JavaScript means.

Does it have any sense?


Robert's Avatar
Robert
Guest
n/a Posts
December 23rd, 2005
09:45 AM
#21

Re: JSON without AJAX
Randy Webb wrote:[color=blue]
> ...
> And unreliable is not
> relative. If two things are both unreliable, then they are equally
> unreliable.[/color]

A has a 10% chance of failing, and B has a 90% chance of failing.
A and B are equally unreliable??

VK's Avatar
VK
Guest
n/a Posts
December 23rd, 2005
10:05 AM
#22

Re: JSON without AJAX
Addon to the previous post <Message-ID:
<1135333546.132573.127670@z14g2000cwz.googlegroups. com>

That should be clear that this is not the question of *inventing*
standards. The posed questions are connected to the core browser
functionality, therefore each existing browser with script support
already has some standard implemented in its source code - with all
if-else branches and priority flags. Just no one throught up-to-date
necessary to share it with the public. So it needs to be done by the
public themselves.
IMHO


bwucke@gmail.com's Avatar
bwucke@gmail.com
Guest
n/a Posts
December 23rd, 2005
10:15 AM
#23

Re: JSON without AJAX

VK napisal(a):[color=blue]
> Thomas 'PointedEars' Lahn wrote:[color=green]
> > "Reliable" means it works under _all_ circumstances.[/color]
>
> Then let's define exactly what "reliable" would mean in this case and
> what circumstances should be expected.
>
> (pseudo-code)
>
> Variant 1 - hardcoded kill
> [remove existing node] scriptObject
> [add new node] scriptObject
> [assign] scriptObject.src = newURL
>
> Does removing the node devalidate the current script context of
> scriptObject - so it would be free for garbage collection?
> What moment such devalidation occurs (if it does) - immediately upon
> removing the node? somewhere later?[/color]

I'd say, never recycle deleted object (explicitly, at least. Just let
the browser and OS handle all recycling.). Assign an unique identifier
to each imported script, so even loading the same script twice won't
result in overwriting one instance by the other. Never reuse discarded
script object. This way you don't have to worry about it anymore. You
dump it in the garbage can and don't worry if garbagemen arrive in a
moment or tomorrow, it's not of your concern. And if never, then we
have a mild memory leak. Let's first handle script loading and fix the
leak (if any) later (say, by traversing the tree of the loaded script
and deleting each leaf separately, if we have to.)
[color=blue]
> What moment we can try to access new scriptObject? When the
> text/javascript input stream is closed?[/color]

a bet that looks safe is what is located on the bottom of the script
gets loaded last. Just add setting a marker variable to the bottom of
the file and poll it, or even trigger an event, or just launch a
predefined 'perma-script' function by a toplevel call on the bottom of
the loaded file.

--perma-script--
function loadScript(newURL)
{
[set and export new seed/mark/identifier/whatever.]
[create node]
[assign] scriptObject.src = newURL
return;
}
// now the script is loading.
function onAfterScriptLoad()
{
//now we assume the script is loaded.

}

--loadable-script--
//...
// lots of stuff here, whatever we want loaded.
//...

loaderscript.onAfterScriptLoad();
--end-of-file--

I think a pretty secure metod of caging the loaded scripts would be
loading them into dynamically created hidden iframe subdocuments.
[color=blue]
> When interpreter finished some
> tuneup job somether after? Or even while text/javascript input stream
> is still open?[/color]

even if it is, everything we needed is already parsed and loaded.
[color=blue]
> If newURL doesn't exists or is not reachable: do we have any formal
> signs to see the trouble?[/color]

We never get the "I'm alive" reply from the child script. We could set
a timeout to try surviving that. Worse problem if the script is
corrupted (but since we likely have access to it as literal string, we
should be able to check some hash if we want to... and if we survive
the parser error.)
[color=blue]
> Variant 2 - soft replacement
> Same questions but getting even more tricky.
> I doubt very much that there are any written standards for it - but I
> can be crossly mistaken.[/color]

I'm not even sure if/how legal is it.
Assume script changes its own src, then continues running. A live,
active, currently running function gets deleted/garbage-collected?


Jasen Betts's Avatar
Jasen Betts
Guest
n/a Posts
December 23rd, 2005
10:25 AM
#24

Re: JSON without AJAX
On 2005-12-22, VK <schools_ring@yahoo.com> wrote:[color=blue]
>
> Randy Webb wrote:[color=green]
>> Thomas 'PointedEars' Lahn said the following on 12/22/2005 2:32 PM:
>>
>>[color=darkred]
>> >That said, "loading scripts" after the document was loaded is still unreliable.[/color]
>>
>> No more unreliable than trying to load them when the page is loading.
>>[/color]
>
> if (scriptObject.readyState == 'complete')
> in IE works just fine and reliable - at least seems so, I did not check
> it under all circumstances.
>
> It is a shame that others did not implement anything so convenient and
> obvious - except for XMLHttpRequest - they must be thinking that after
> such glorious achievement they can take a rest for another year or two.
>:-)[/color]

onload works here. (Mozilla 1.7.8)

function ha(){alert("ha!");};

function pastejs(){
s = document.createElement("script");
s.onload=ha;
s.type = "text/javascript";
s.src = "test.js";
document.body.appendChild(s);
}


Bye.
Jasen

bwucke@gmail.com's Avatar
bwucke@gmail.com
Guest
n/a Posts
December 23rd, 2005
11:15 AM
#25

Re: JSON without AJAX
Robert napisal(a):[color=blue]
> Randy Webb wrote:[color=green]
> > ...
> > And unreliable is not
> > relative. If two things are both unreliable, then they are equally
> > unreliable.[/color]
>
> A has a 10% chance of failing, and B has a 90% chance of failing.
> A and B are equally unreliable??[/color]

I think Randy meant - "almost". 10% is so huge unreliablity that
between it and 90% the difference is marginal. Would you pick an
airplane that falls in 90% cases or one that falls in 10% cases - or
would you rather go by bus, facing such a choice? And what is "reliable
enough"? 1%? 0.01%? There's little sense in pondering that. Either
something is completely reliable (that is, given -all- the
prerequisites it demands, it will -never- fail), or we still use it IF
we can recover from a crash gracefully and the consequences are
acceptable (say, it may fail in 90% cases, but we can freely and
automatically retry until it succeeds... then it's perfectly reliable
for us.)

Anyway, tiny proofOfConcept code. Loads js entered in the box. Works in
FF 1.5.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html><head><title>The scriptload kitchen</title>

<script type="text/javascript">

function run(url)
{
newscr=document.createElement('script');
newscr.setAttribute("src",url)
document.body.appendChild(newscr);
}

</script>


</head><body>
<form name="inp" method="post" action="#">
<input type="text" value="http://www.kurs.horsesport.pl/inne/test.js"
name="txt">
<input type="button" value="run!"
onClick="run(document.inp.txt.value);" >
</form></body></html>


Thomas 'PointedEars' Lahn's Avatar
Thomas 'PointedEars' Lahn
Guest
n/a Posts
December 23rd, 2005
12:05 PM
#26

Re: JSON without AJAX
Join Bytes! wrote:
[color=blue]
> Anyway, tiny proofOfConcept code.[/color]

That is no proof of concept whatsoever.
[color=blue]
> Loads js entered in the box. Works in FF 1.5.[/color]

It was not debated that it works in some browsers on certain occasions
with certain pieces of code. I have written such a function long
before but attached the necessary "caveat" section to its documentation.
[color=blue]
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">[/color]

No system identifier, triggering Quirks Mode.
[color=blue]
> <html><head><title>The scriptload kitchen</title>[/color]

No charset declaration, rendering this invalid if not served via HTTP.
But then, the document itself is already not Valid HTML 4.0.
[color=blue]
> <script type="text/javascript">
>
> function run(url)
> {
> newscr=document.createElement('script');[/color]
^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^
Undeclared global variable; calling a (DOM) method without previous
feature-test.
[color=blue]
> newscr.setAttribute("src",url)[/color]

Not testing the return value of the (DOM) method called previously.

No need for setAttribute(), also standardized `newscr.src = ...'
suffices in all cases.
[color=blue]
> document.body.appendChild(newscr);
> }[/color]

If, for example, I put document.write() somewhere in that script, the
document would be overwritten since it was already loaded. If we are
talking about JSON data instead, then it /may/ work.

Again: It was not debated that it is possible to append `script' elements
after the document was loaded; what was debated was that the outcome would
always be useful, and that it is possible to determine when or whether the
data was fully loaded.


PointedEars

Robert's Avatar
Robert
Guest
n/a Posts
December 23rd, 2005
02:05 PM
#27

Re: JSON without AJAX
Join Bytes! wrote:[color=blue]
> Robert napisal(a):
>[color=green]
>>Randy Webb wrote:[color=darkred]
>> > ...[/color]
>>[color=darkred]
>>>And unreliable is not
>>>relative. If two things are both unreliable, then they are equally
>>>unreliable.[/color]
>>
>>A has a 10% chance of failing, and B has a 90% chance of failing.
>>A and B are equally unreliable??[/color]
>
>
> I think Randy meant - "almost". 10% is so huge unreliablity that
> between it and 90% the difference is marginal. Would you pick an
> airplane that falls in 90% cases or one that falls in 10% cases[/color]

True, but it doesn't change the fact that the are not equally
unreliable. If for some reason you are forced to take one of these
planes which one would you take?
If they are (almost) equally unreliable it sounds like you don't have
much of a preference :p

bwucke@gmail.com's Avatar
bwucke@gmail.com
Guest
n/a Posts
December 23rd, 2005
02:35 PM
#28

Re: JSON without AJAX

Thomas 'PointedEars' Lahn wrote:[color=blue]
> Join Bytes! wrote:
>[color=green]
> > Anyway, tiny proofOfConcept code.[/color]
>
> That is no proof of concept whatsoever.[/color]

It is. Proof that in -certain- cases it's possible. Means it makes
sense to move on. If it didn't work at all, ever, further debating
would be pointless. We'd have to move on to a different approach at
least.
[color=blue][color=green]
> > Loads js entered in the box. Works in FF 1.5.[/color]
>
> It was not debated that it works in some browsers on certain occasions
> with certain pieces of code.[/color]

I think it was. Two suggested approaches, one of them I think bound to
fail ("soft loading") and the other working at least in some cases, as
proven by example.
[color=blue]
> I have written such a function long
> before but attached the necessary "caveat" section to its documentation.[/color]

Why didn't you post it then?!?
[color=blue]
> No system identifier, triggering Quirks Mode.
> No charset declaration, rendering this invalid if not served via HTTP.
> But then, the document itself is already not Valid HTML 4.0.
> Undeclared global variable; calling a (DOM) method without previous
> feature-test.
> Not testing the return value of the (DOM) method called previously.
> No need for setAttribute(), also standardized `newscr.src = ...'
> suffices in all cases.[/color]

Agreed. Dirty code.
Still, it worked. Can be done, point proven, purpose of the code
achieved,how - not important.
Time to move on to cleanup of the code, testing compatiblity with other
browsers and implementing further features. Your turn? Supposedly, you
have the code already.
[color=blue]
> If, for example, I put document.write() somewhere in that script, the
> document would be overwritten since it was already loaded. If we are
> talking about JSON data instead, then it /may/ work.[/color]

There's more than one way you can break your own scripts.
document.write() works on the HTML text level. Probably this approach
(as well as innerHTML, textContent etc) would have to be replaced by
direct DOM tree calls (appendChild, createTextNode etc.)
[color=blue]
> Again: It was not debated that it is possible to append `script' elements
> after the document was loaded; what was debated was that the outcome would
> always be useful, and that it is possible to determine when or whether the
> data was fully loaded.[/color]

Nope. In case of static text documents it wouldn't be useful, for sure.
In case of browsers not supporting Javascript it would not be useful.
Nothing is -always- useful. All we can hope for is, it will be robust
enough to be useful enough for our intended purposes. and we can learn
the level of usefulness.

So far you keep attacking whoever does a tiniest step outside the
rules, no matter what the general direction and what outcomes of their
step. Syntax nazi, standards nazi, netiquette nazi, but you seem
extremely reluctant to provide solutions yourself. Instead, whoever
says something new, you jump in, saying "I did it a long time ago,
better. You're dumb and I'm smart".

Maybe it's time to provide some actual content?


Thomas 'PointedEars' Lahn's Avatar
Thomas 'PointedEars' Lahn
Guest
n/a Posts
December 23rd, 2005
02:55 PM
#29

Re: JSON without AJAX
Join Bytes! wrote:
[color=blue]
> Thomas 'PointedEars' Lahn wrote:[color=green]
>> Join Bytes! wrote:[color=darkred]
>> > Anyway, tiny proofOfConcept code.[/color]
>> That is no proof of concept whatsoever.[/color]
>
> It is. Proof that in -certain- cases it's possible. [...][/color]

But that was not what was to be proven.
[color=blue][color=green][color=darkred]
>> > Loads js entered in the box. Works in FF 1.5.[/color]
>>
>> It was not debated that it works in some browsers on certain occasions
>> with certain pieces of code.[/color]
>
> I think it was.[/color]

No, that was not debated. Read again.
[color=blue]
> [...][color=green]
>> I have written such a function long
>> before but attached the necessary "caveat" section to its documentation.[/color]
>
> Why didn't you post it then?!?[/color]

Why should I post something that I cannot even recommend?
[color=blue][color=green]
>> Again: It was not debated that it is possible to append `script' elements
>> after the document was loaded; what was debated was that the outcome
>> would always be useful, and that it is possible to determine when or
>> whether the data was fully loaded.[/color]
>
> Nope. In case of static text documents it wouldn't be useful, for sure.
> In case of browsers not supporting Javascript it would not be useful.[/color]

And in case W3C DOM Level 2 HTML would not be supported as supposed or
the unspecified behavior that `script' element's contents is passed to
the script engine after the document has finished loading was not there.
And ...
[color=blue]
> [...]
> So far you keep attacking whoever does a tiniest step outside the
> rules, no matter what the general direction and what outcomes of their
> step. Syntax nazi, standards nazi, netiquette nazi, [...][/color]

Godwin's Law. You lose. And *PLONK*


PointedEars

VK's Avatar
VK
Guest
n/a Posts
December 23rd, 2005
03:55 PM
#30

Re: JSON without AJAX

Thomas 'PointedEars' Lahn wrote:[color=blue]
> If, for example, I put document.write() somewhere in that script, the
> document would be overwritten since it was already loaded.[/color]

This is why scriptObject.defer = true has been introduced and it's
being currently supported by all browsers I can get on hold of from my
windowed viewport. That means that the Mac world as well as the Unix
branch of evolution (Linux, Free BSD end Co.) is forcely is out of my
investigation. Volunteers are badly needed from the parallel worlds but
they seem to be indifferent to the project.

In the Windows Reality I'm locked in - the statement scriptObject.defer
= true seems to be the commonly accepted way to inform the interpreter
that the code to load will not change anyhow (or nullify) the existing
DOM structure. The acceptance of this flag among all browser versions
is still a subject of further investigation.


Michael Winter's Avatar
Michael Winter
Guest
n/a Posts
December 23rd, 2005
04:45 PM
#31

Re: JSON without AJAX
On 23/12/2005 16:47, VK wrote:

[snip]
[color=blue]
> In the Windows Reality I'm locked in - the statement scriptObject.defer
> = true seems to be the commonly accepted way to inform the interpreter
> that the code to load will not change anyhow (or nullify) the existing
> DOM structure.[/color]

The presence of the defer attribute doesn't mean that a script won't
modify the document tree. It is a /hint/ to the user agent that the
script can be executed at its leisure; that execution can be deferred.

The use of the defer attribute wouldn't be appropriate for a script that
calls the document.write method because the output from that call is
usually meant to be inserted just after the SCRIPT element that contains
it. If such a call were deferred, the output could possibly occur
anywhere, and that its position might even change on subsequent visits
(depending if and how a user agent chooses to defer).
[color=blue]
> The acceptance of this flag among all browser versions is still a
> subject of further investigation.[/color]

It is, and it might not even be possible to determine that a particular
browser definitely does not defer (false negatives). A simple test could
look something like:

<script type="text/javascript">
var deferred = true;
</script>
<script type="text/javascript" defer>
deferred = false;
</script>
<script type="text/javascript">
alert('Deferred: ' + deferred);
</script>

and does indeed suggest that IE will defer execution.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.

VK's Avatar
VK
Guest
n/a Posts
December 23rd, 2005
06:55 PM
#32

Re: JSON without AJAX

Michael Winter wrote:[color=blue]
> It is a /hint/ to the user agent that the
> script can be executed at its leisure; that execution can be deferred.[/color]

*Any* trustwordy links about "defer" attribute? The best I've found so
far were exactly about the point: "a script with defer set to true will
deferred".
However sufficien from a "normal" point of view it would be, are there
any "abnormal" but more multiwordy comments about it?


Thomas 'PointedEars' Lahn's Avatar
Thomas 'PointedEars' Lahn
Guest
n/a Posts
December 23rd, 2005
09:35 PM
#33

Re: JSON without AJAX
VK wrote:
[color=blue]
> Michael Winter wrote:[color=green]
>> It is a /hint/ to the user agent that the
>> script can be executed at its leisure; that execution can be deferred.[/color]
>
> *Any* trustwordy links about "defer" attribute?[/color]

Yes, the HTML 4.01 Specification. It makes deferring such scripts a
possibility, not a recommendation or even a necessity.
[color=blue]
> The best I've found so
> far were exactly about the point: "a script with defer set to true will
> deferred".[/color]

Which most certainly is from the MSDN Library or another IE-related
resource.


PointedEars

Jasen Betts's Avatar
Jasen Betts
Guest
n/a Posts
December 23rd, 2005
10:25 PM
#34

Re: JSON without AJAX
On 2005-12-23, Randy Webb <HikksNotAtHome@aol.com> wrote:

[color=blue]
> What is not? The convenience of readyState? The obviousness of
> readyState? Or that it is a shame? It is a shame indeed that no other
> browser gives you a method to tell when a script has loaded.[/color]

mozilla has onload.


Bye.
Jasen

VK's Avatar
VK
Guest
n/a Posts
December 24th, 2005
09:55 AM
#35

Re: JSON without AJAX
See also Dynodes Project at <http://www.mindsack.com/uxe/dynodes/>
and the quoted vot.sep. of JSON's inventor Douglas Crockford.

IMHO security issues with cross-domain script interchange is
over-appreciated. There are much more effective ways to bypass
same-domain limitations with malicious purposes. JavaScript adds
absolutely nothing extra in the pucture.

There are not any doable ways to have *full* browsing experience
*everywhere* across the Web in *absolutely secure* environment. There
are means to set security settings by domains, but this task is
accomplished on the higher level (security zones) and JavaScript per se
doesn't interfer neither affect to that.


Michael Winter's Avatar
Michael Winter
Guest
n/a Posts
December 24th, 2005
12:45 PM
#36

Re: JSON without AJAX
On 23/12/2005 19:42, VK wrote:
[color=blue]
> *Any* trustwordy links about "defer" attribute?[/color]

Trustworthy? There's nothing wrong with the W3C documentation in this
regard:

defer
When set, this boolean attribute provides a hint to the user
agent that the script is not going to generate any document
content (e.g., no "document.write" in javascript) and thus,
the user agent can continue parsing and rendering.
-- 18.2.1 The SCRIPT element, HTML 4.01 [1]

particularly when Microsoft's documentation says much the same (so no
threat to your 'reality'):

Using the attribute at design time can improve the download
performance of a page because the browser does not need to
parse and execute the script and can continue downloading and
parsing the page instead.
-- Remarks, DEFER Attribute | defer Property, MSDN [2]

It might be worth mentioning that the generating "document content"
comment above needn't apply to the DOM methods that operate at a node
level. As I wrote previously, the document.write method relies on the
positioning of the SCRIPT element that calls it