This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#126 2012-10-04 04:27:58

trinary
Scratcher
Registered: 2012-01-29
Posts: 1000+

Re: Javascript topic

Is there any way associative arrays could be created ?

Last edited by trinary (2012-10-04 04:28:40)


http://trinary.tk/images/signature_.php

Offline

 

#127 2012-10-04 05:42:24

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Javascript topic

Sure, just use an object:

Code:

myAA = {};

function setKeyValuePair(key, val, AA) {
 AA[key]=val;
}
function deleteKeyValuePair(key, AA) {
  AA[key] = undefined;
}
function getValue(key, AA) {
  return AA[key];
}

Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#128 2012-10-04 21:22:02

trinary
Scratcher
Registered: 2012-01-29
Posts: 1000+

Re: Javascript topic

Thank you.  big_smile


http://trinary.tk/images/signature_.php

Offline

 

#129 2012-10-06 03:53:25

trinary
Scratcher
Registered: 2012-01-29
Posts: 1000+

Re: Javascript topic

Is there any way images can be uploaded in an AJAX-like fashion (without reloading the page) without using jQuery?


http://trinary.tk/images/signature_.php

Offline

 

#130 2012-10-06 07:17:07

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Javascript topic

Yeah. Create a canvas, paste the image onto it, get the imagedata, and upload it as a string. On the server-side, get the contents of that image.  smile

Code:

var c = document.createElement("canvas")
var ctx = c.getContext("2d")
var i = new Image();
i.src = "image-source-here";
i.onload=function() {
 ctx.drawImage(i);
 d = c.toDataURL();
  // send d via AJAX
}

Code:

# python client-side
# ...get contents of POST request, store it in variable d here...
import urllib
img = urllib.urlopen(d).read()
# do something with img!

Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#131 2012-10-06 10:08:44

rias
Scratcher
Registered: 2011-08-27
Posts: 100+

Re: Javascript topic

is there a script to another page,
if "?" behind the url of the page?


http://i.imgur.com/PmIaxNZ.gif

Offline

 

#132 2012-10-06 10:37:06

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: Javascript topic

rias wrote:

is there a script to another page,
if "?" behind the url of the page?

Yes, but that isn't Javascript, that's PHP.

Offline

 

#133 2012-10-06 12:44:53

rias
Scratcher
Registered: 2011-08-27
Posts: 100+

Re: Javascript topic

okay


http://i.imgur.com/PmIaxNZ.gif

Offline

 

#134 2012-10-06 20:14:04

transparent
Scratcher
Registered: 2011-04-19
Posts: 1000+

Re: Javascript topic

Have you guys ever used this?:

Code:

javascript:document.body.contentEditable = 'true'; document.designMode='on'; void 0

yes, yes i do.

Offline

 

#135 2012-10-07 22:22:58

trinary
Scratcher
Registered: 2012-01-29
Posts: 1000+

Re: Javascript topic

Hardmath123 wrote:

Yeah. Create a canvas, paste the image onto it, get the imagedata, and upload it as a string. On the server-side, get the contents of that image.  smile

Code:

var c = document.createElement("canvas")
var ctx = c.getContext("2d")
var i = new Image();
i.src = "image-source-here";
i.onload=function() {
 ctx.drawImage(i);
 d = c.toDataURL();
  // send d via AJAX
}

Code:

# python client-side
# ...get contents of POST request, store it in variable d here...
import urllib
img = urllib.urlopen(d).read()
# do something with img!

Thank you. A lot.  big_smile


http://trinary.tk/images/signature_.php

Offline

 

#136 2012-10-07 22:24:52

trinary
Scratcher
Registered: 2012-01-29
Posts: 1000+

Re: Javascript topic

transparent wrote:

Have you guys ever used this?:

Code:

javascript:document.body.contentEditable = 'true'; document.designMode='on'; void 0

It can be occasionally useful.


http://trinary.tk/images/signature_.php

Offline

 

#137 2012-10-07 22:54:17

technoguyx
Scratcher
Registered: 2008-10-18
Posts: 1000+

Re: Javascript topic

Recently I've been fiddling with the Google Apps Script JavaScript API, pretty useful for messing with documents and making web apps.  tongue  Anyone here heard of it? Still figuring out some aspects of it, though it's pretty simple.

Last edited by technoguyx (2012-10-07 22:55:31)


http://getgnulinux.org/links/en/linuxliberated_4_78x116.png

Offline

 

#138 2012-10-07 23:43:49

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Javascript topic

trinary wrote:

transparent wrote:

Have you guys ever used this?:

Code:

javascript:document.body.contentEditable = 'true'; document.designMode='on'; void 0

It can be occasionally useful.

i prefer inspect element


Posts: 20000 - Show all posts

Offline

 

#139 2012-10-07 23:45:18

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Javascript topic

veggieman001 wrote:

trinary wrote:

transparent wrote:

Have you guys ever used this?:

Code:

javascript:document.body.contentEditable = 'true'; document.designMode='on'; void 0

It can be occasionally useful.

i prefer inspect element

Inspect element is better.
just summing your post :3


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#140 2012-10-07 23:53:40

trinary
Scratcher
Registered: 2012-01-29
Posts: 1000+

Re: Javascript topic

veggieman001 wrote:

trinary wrote:

transparent wrote:

Have you guys ever used this?:

Code:

javascript:document.body.contentEditable = 'true'; document.designMode='on'; void 0

It can be occasionally useful.

i prefer inspect element

I prefer Firebug.


http://trinary.tk/images/signature_.php

Offline

 

#141 2012-10-07 23:54:18

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Javascript topic

jji7skyline wrote:

veggieman001 wrote:

trinary wrote:

It can be occasionally useful.

i prefer inspect element

Inspect element is better.
just summing your post :3

:@@@@
so what you're saying is what i prefer is the best


Posts: 20000 - Show all posts

Offline

 

#142 2012-10-08 00:04:32

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: Javascript topic

Hardmath123 wrote:

Sure, just use an object:

Code:

myAA = {};

function setKeyValuePair(key, val, AA) {
 AA[key]=val;
}
function deleteKeyValuePair(key, AA) {
  AA[key] = undefined;
}
function getValue(key, AA) {
  return AA[key];
}

erm whats wrong with delete AA[key] ? its more logical [ unless you use the strict mode ]


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#143 2012-10-08 00:07:37

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: Javascript topic

trinary wrote:

Is there any way images can be uploaded in an AJAX-like fashion (without reloading the page) without using jQuery?

Check out the new api called fileSystem API its awesome and wont need to use canvas its very very simple aswell here is what i do

1. Open the file as filestream <input type="file"> or drag and drop api
2. Create a new FormData();
3. Append that file to the form data and "post" it to the server via xhr..

in older days a file type input used to do this. but file API Rocks , you can even read file on client side as binary stream and do binary magic with it  big_smile 


why is this better ??

To data url is base64 and depending on the encoding used for transporting it its size can be n times larger then the actual file , which we dont need .. esp when we can directly send files!


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#144 2012-10-08 00:11:00

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: Javascript topic

Hardmath123 wrote:

Sure, just use an object:

Code:

myAA = {};

function setKeyValuePair(key, val, AA) {
 AA[key]=val;
}
function deleteKeyValuePair(key, AA) {
  AA[key] = undefined;
}
function getValue(key, AA) {
  return AA[key];
}

JavaScript array's are associative by default why even do that ?? I mean seriously
..

Code:

 var arr = new Array();
 arr['foo'] = 'bar';
 delete arr['foo'];

but if we go in depth javascript arrays are just objects with some prototype methods for length push and pop shift and unshift so well depends really on what he wanna choose by the way @Hardmath here is a trick..

Code:

function AssocArray(){
 if( !(this instanceof  AssocArray)){
    return new AssocArray(); // good old trick to save pain we got from forgetting a new
 }
 var arr = {};
 this.setKeyValue = function(key,value){
   arr[key] = value;
 }
 this.unsetKeyValue = function(key){
  if(arr[key])
  delete arr[key];
 }
 this.getKeyValue = function(key){
  return arr[key]
 }
}

Last edited by fanofcena (2012-10-08 00:17:46)


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#145 2012-10-08 00:30:30

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Javascript topic

veggieman001 wrote:

jji7skyline wrote:

veggieman001 wrote:


i prefer inspect element

Inspect element is better.
just summing your post :3

:@@@@
so what you're saying is what i prefer is the best

What you prefer isn't automatically the best, but in this case, it is.


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#146 2012-10-08 01:21:24

trinary
Scratcher
Registered: 2012-01-29
Posts: 1000+

Re: Javascript topic

fanofcena wrote:

Hardmath123 wrote:

Sure, just use an object:

Code:

myAA = {};

function setKeyValuePair(key, val, AA) {
 AA[key]=val;
}
function deleteKeyValuePair(key, AA) {
  AA[key] = undefined;
}
function getValue(key, AA) {
  return AA[key];
}

JavaScript array's are associative by default why even do that ?? I mean seriously
..

Code:

 var arr = new Array();
 arr['foo'] = 'bar';
 delete arr['foo'];

but if we go in depth javascript arrays are just objects with some prototype methods for length push and pop shift and unshift so well depends really on what he wanna choose by the way @Hardmath here is a trick..

Code:

function AssocArray(){
 if( !(this instanceof  AssocArray)){
    return new AssocArray(); // good old trick to save pain we got from forgetting a new
 }
 var arr = {};
 this.setKeyValue = function(key,value){
   arr[key] = value;
 }
 this.unsetKeyValue = function(key){
  if(arr[key])
  delete arr[key];
 }
 this.getKeyValue = function(key){
  return arr[key]
 }
}

Thank you.  big_smile
I did not know that.


http://trinary.tk/images/signature_.php

Offline

 

#147 2012-10-08 01:49:20

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Javascript topic

fanofcena wrote:

Hardmath123 wrote:

Sure, just use an object:

Code:

myAA = {};

function setKeyValuePair(key, val, AA) {
 AA[key]=val;
}
function deleteKeyValuePair(key, AA) {
  AA[key] = undefined;
}
function getValue(key, AA) {
  return AA[key];
}

JavaScript array's are associative by default why even do that ?? I mean seriously
..

Code:

 var arr = new Array();
 arr['foo'] = 'bar';
 delete arr['foo'];

but if we go in depth javascript arrays are just objects with some prototype methods for length push and pop shift and unshift so well depends really on what he wanna choose by the way @Hardmath here is a trick..

Code:

function AssocArray(){
 if( !(this instanceof  AssocArray)){
    return new AssocArray(); // good old trick to save pain we got from forgetting a new
 }
 var arr = {};
 this.setKeyValue = function(key,value){
   arr[key] = value;
 }
 this.unsetKeyValue = function(key){
  if(arr[key])
  delete arr[key];
 }
 this.getKeyValue = function(key){
  return arr[key]
 }
}

I just posted my example functions as a demo to show how the JS objects syntax works. I wouldn't use those functions myself, they're ugly (especially the delete one! thanks for that tip), but it suffices to show what I meant.  smile  Also, I don't think you really need a separate class for associative arrays, the traditional syntax is good enough.

@trinary also, you should know what JSON is. JSON (JS Object Notation) is used to basically build an anonymous associative array (object in JS lingo, and dictionary in other languages). It's a lot like XML:

Code:

{"key":"value"}
{"key":{"nested":"value"}}
{"key":["array", "with", "four", "objects"]}
...

Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#148 2012-10-08 04:42:28

trinary
Scratcher
Registered: 2012-01-29
Posts: 1000+

Re: Javascript topic

Interesting. Thank you.  smile


http://trinary.tk/images/signature_.php

Offline

 

#149 2012-10-08 06:13:25

transparent
Scratcher
Registered: 2011-04-19
Posts: 1000+

Re: Javascript topic

trinary wrote:

transparent wrote:

Have you guys ever used this?:

Code:

javascript:document.body.contentEditable = 'true'; document.designMode='on'; void 0

It can be occasionally useful.

Well, I never use it because of inspect, but I always thought it was cool when I was first learning it.  smile


yes, yes i do.

Offline

 

#150 2012-10-15 22:39:43

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: Javascript topic

transparent wrote:

trinary wrote:

transparent wrote:

Have you guys ever used this?:

Code:

javascript:document.body.contentEditable = 'true'; document.designMode='on'; void 0

It can be occasionally useful.

Well, I never use it because of inspect, but I always thought it was cool when I was first learning it.  smile

I have used the contenteditable attribute though to create custom facebook like inputs :-) its cool.. allows u to do things that a normal textbox or input cant [ rich input  big_smile  ]


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

Board footer