İpuçlarım } Serkan Konakcı


Go Go İş, olsun yeterki! Engeller aşılır…

Archives

Graphic — No Comments
17
Mar 11

Documentation About the Most Quirky Parts of Javascript

Documentation About the Most Quirky Parts of Javascript: ”

JavaScript Garden is a growing collection of documentation about the most quirky parts of the JavaScript programming language. It gives advice to avoid common mistakes, subtle bugs, as well as performance issues and bad practices that non-expert JavaScript programmers may encounter on their endeavours into the depths of the language.

JavaScript Garden does not aim to teach you JavaScript. Former knowledge of the language is strongly recommended in order to understand the topics covered in this guide. It is published under the MIT license and hosted on GitHub.

javascript-garden

Requirements: -
Demo: http://bonsaiden.github.com/JavaScript-Garden/
License: MIT License

Related Posts

  • How to Create GitHub Style Buttons with CSS & Javascript
  • Jasmine – A JavaScript Testing Framework
  • Learning Web Standards with Opera
  • The Seven Rules of Unobtrusive Javascript
  • Merb – MVC Framework Like Ruby on Rails but Simpler
Graphic — No Comments
07
Jul 10

Tüm Dış Linkleri Yeni Pencerede Açtırmak

This little bit of code, using JQuery, will parse every anchor

<a href="#"></a>

on your page, decide if it points to an external site. If it does, will instruct it to open in a new window.

// Open external links in new windows using jquery
$('a[href^="http://"]').filter(function() {
    return this.hostname && this.hostname !== location.hostname;
}).attr('target', '_blank');

I personally find it very useful when working in xHTML Strict mode as the the

target=""

attribute is no longer a valid property and will cause your page to return as invalid.

*Remember to include Jquery.js in the header of your page.

Graphic — 1 Comment
11
Sep 09

Javascript ile Cookie Yönetimi

Cookieler sabit diskimizde yer alan basit bir text dosyasıdır. Cookieler siteleri ziyaret ettiğimizde otomatik olarak yaratılır ve bizim ziyaretimiz hakkında bilgi tutarlar.

Hangi bilgileri tutacağına webmaster karar verir. Cookieler genellikle kullanıcıların varsa daha önceki ziyareti esnasında girmiş olduğu bilgilerini, yapmış olduğu alışverişlerini gibi bazı bilgileri tutabilir. Javascript ile Cookie atayabiliriz.

Javascript ile Cookie Yaratma

Aşağıdaki kodu çalıştırdıktan sonra Cookie klasöründe bir adet dosya oluşacaktır. Eğer cookie yaratılmamışsa Expires değerini şuanki tarihten ileri bir tarihe veriniz. Ayrıca başka bir değişken de atayabilirsiniz (Örneğin “deger2=”).

<script language="javascript" type='text/javascript'>
	document.cookie =
	"myContents=Quackit JavaScript cookie experiment;
	expires=Fri, 19 Oct 2009 12:00:00 UTC; path=/";
</script>

Javascript ile Cookie Okumak

<script language="javascript" type='text/javascript'>
	document.write(document.cookie);
</script>

Yukarıdaki kod ile kolayca Cookiler okunabilir. Ancak herbir değişkeni tek tek elde etmek için

<script language="javascript" type='text/javascript'>
	document.write(document.cookie.split(";")[1])
</script>

şeklinde yazmak gerekmektedir.

Javascript ile Cookieleri Silmek

Yapılması gereken şey çok basit cookieyi geçmiş bir tarihe set etmek:

<script language="javascript" type='text/javascript'>
    document.cookie =
    "myContents=Quackit JavaScript cookie experiment;
	expires=Fri, 14 Oct 2005 12:00:00 UTC; path=/";
</script>

Cookie Yönetim Scripti

Aşağıdaki script cookieleri oluşturmayı, okumayı ve silmeyi sağlayan küçük bir uygulama örneğidir.

function createCookie(name,value,days) {
if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
}
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}
Web — No Comments
01
Sep 09

Microsoft Web Dünyası

Microsoft Expression 3

Microsoft’un web geliştiricileri için piyasaya sürdüğü bazı uygulamalar var. Bunlardan biri Microsoft Expression Web 3.0, daha önceden 2.0 sürümünü denediğim bu uygulamadan pek de hoşlanmadım. Dreamweaver gibi bir IDE’niz olduğu sürece pek de ihtiyaç yok gibi.  Ama yinede göz gezdirmekte ve tasarımına bakmakda fayda var. Ücretli bir yazılım olarak sunulmaktadır fakat 60 günlük ücretsiz deneme sürümü indirilip kullanılabilir.

expression

http://www.microsoft.com/expression/try-it/Default.aspx?filter=web3

Microsoft Web Platform

Bununla beraber Microsoft Web Platform ortamını da denemekte fayda görüyorum. Beraberinde bir çok ücretsiz kurulumu ve ugulamayı da getirmektedir. En kötü ihtimalle IIS ‘inin versiyonunu benim gibi yükseltme ihtiyacı duyacak arkadaşlar için işe yarar görünüyor.

webplatform

http://www.microsoft.com/web/downloads/platform.aspx

Micrsoft Visual Studio Express 2008

Visual Studio Express Edition kullanılmadan önce son IIS, Asp.NET updatelerinin ve extensionlarının kurulu olması gerekmektedir.  Bu güncellemerin ve eklentilerin hepsi yukarıda belirtiğim Microsft Web Platform sayesinde makinaya kurulabilir. İlk kurulumda MSDN kütüphanesi gelmemesine karşın download edilerek sonradan kurulabilmektedir.

visualstudioexpress

http://www.microsoft.com/express/download/

Graphic — No Comments
25
Jul 09

JavaScript Scope ve Closures

Nesne programlamanın mantığında bilinmesi gereken temel şeylerden birisi Scope olayıdır. Scope bir fonksiyonun sınırlarını belirlemede kullanılır. Fonksiyon içerisinde çalıştırılacak içeriği ve bu fonksiyona atanan değişkenlerin global ya da local olma sınırılarını belirler.

Global Scope

Her yerden ulaşılabilinen elemanlar bu alan içerisinde tanımlanmalıdır.

	var deneme = “değişken”;
	function global(){
		alert(“Merhaba Limaks Atlantis Otel”);
	}

Local Scope

Local Scope belirli bir alan içerisinde çalışan elemanları temsil etmektedir.

	function local(){
		var deneme = “değişken”;
		return alert(deneme);
	}
	alert(deneme); //hata verecektir

Yukarıdaki koda bakacak olursak deneme isimli değişken sadece local fonksiyonu içerisinde tanımlanmıştır. Bu nedenle fonksiyon dışında hata verecektir. Ancak dikkat edilmesi gereken bir husus daha var; local değişken tanımlanırken başına “var” ifadesi eklenmez ise değişken global değişken olur.

Graphic — No Comments
10
Jul 09

JavaScript and Object Oriented Programming (OOP)

Credits: This tutorial is written and contributed by Tim Scarfe. Edited by JavaScriptKit.com for content/ structure. Please see footnote for more information on author.

JavaScript is an excellent language to write object oriented web applications. It can support OOP because it supports inheritance through prototyping as well as properties and methods. Many developers cast off JS as a suitable OOP language because they are so used to the class style of C# and Java. Many people don’t realize that JavaScript supports inheritance. When you write object-oriented code it instantly gives you power; you can write code that can be re-used and that is encapsulated.

<IMG border=0 src="http://www.javascriptkit.com/jkincludes/arrow2.gif" width=17 height=17> What’s so great about objects?

Objects work so well because they act just like real life objects- objects have properties and methods. So if we were talking about a lamp, a property of it may be its height or width, say 12cm. A method of it may be to shine (an action). And when it’s shining, its brightness property would be of a greater value than when it wasn’t.

JavaScript gives you the ability to make your own objects for your own applications. With your objects you can code in events that fire when you want them to, and the code is encapsulated. It can be initialized any amount of times.

<IMG border=0 src="http://www.javascriptkit.com/jkincludes/arrow2.gif" width=17 height=17> Creating objects using new Object()

There are several ways to create objects in JavaScript, and all of them have their place. The simplest way is to use the new operator, specifically, new Object():

<script language="javascript" type="text/javascript">
<!--

person = new Object()
person.name = "Tim Scarfe"
person.height = "6Ft"

person.run = function() {
	this.state = "running"
	this.speed = "4ms^-1"
}

//-->
</script>

We define a custom object "person," then add to it its own properties and method afterwards. In this case, the custom method merely initializes two more properties.

<IMG border=0 src="http://www.javascriptkit.com/jkincludes/arrow2.gif" width=17 height=17> Creating objects using Literal Notation

Another inline way of defining an object is via literal notation. Supported in JavaScript1.2 and above, it’s a more robust form of creating an object on the fly:

<script language="javascript" type="text/javascript">
<!--

// Object Literals

timObject = {
	property1 : "Hello",
	property2 : "MmmMMm",
	property3 : ["mmm", 2, 3, 6, "kkk"],
	method1 : function(){alert("Method had been called" + this.property1)}
};

timObject.method1();
alert(timObject.property3[2]) // will yield 3

var circle = { x : 0, y : 0, radius: 2 } // another example

// nesting is no problem.
var rectangle = { 
	upperLeft : { x : 2, y : 2 },
	lowerRight : { x : 4, y : 4}
}

alert(rectangle.upperLeft.x) // will yield 2

//-->
</script>

Literal notion can contain arrays or arbitrary JavaScript expressions or values.

While using the new operator or literal notion to create a custom object is both simple and logical, the biggest shortcoming is that the result is NOT reusable- we cannot easily initialize different versions of the created object. For example with the first demonstration above, if the person’s name is not "Tim Scarfe", we would need to redefine our entire object just to accommodate this change.

From: http://www.javascriptkit.com/javatutors/oopjs.shtml

Graphic — No Comments
07
Jul 09

JavaScript Inheritance

Currently I’m working a fair bit with JavaScript and teaching different ways to use it, and what I want to talk to you about is if and how you use inheritance in JavaScript.

The prototype way

The way inheritance works in JavaScript is that it is prototype-, instead of class-based. For instance, take a look at this:

function Being () {
	this.living = true;
}
Being.prototype.breathes = function () {
	return true;
};

An object which inherits Being:

Robert.prototype = new Being;
function Robert () {
	this.blogs = true;
}
Robert.prototype.getsBored = function () {
	return "You betcha";
};

So, basically, if we create a new instance of the Robert object and call some of its methods, the result would be:

// Create an instance of the Robert object
var me = new Robert();

/*
	Returns "You betcha" as it's a method
	belonging to the Robert object
*/
me.getsBored();

/*
	Returns true. Since the Robert object
	doesn't have a breathes method of
	its own, it goes back in the
	prototype chain to its parent
	object, Being, and finds the
	method there
*/
me.breathes();

Calling a parent’s method

If you want to call a super method of the method currently being executed, i.e. if the same method also exists on the parent object, this is how it would have been done the prototype way:

Robert.prototype.getsBored = function () {
	/*
		Basically, you need to know the name of
		the object and the method name as well.
		Then call it in context
	*/
	Being.prototype.getsBored.call(this);
	return "You betcha";
};

Just to point it out, you could also write: this.constructor.prototype.getsBored.call(this);. The reason for this is that this.constructor points to the main constructor of the object, hence the Being object in this case.

Read more about prototype inheritance

For a more thorough explanation, read about Prototypes and Inheritance – JavaScript: The Definitive Guide.

People mimicking class-based inheritance

A lot of developers, however, come from a class-based inheritance world and either prefer that syntax or just refrain from wanting to use different syntaxes/approaches for different languages. A result of this has been a number of implementations of a more class-based nature in JavaScript, of which these are some of the more well-known ones:

* Simple JavaScript Inheritance – John Resig
* A Base Class for JavaScript Inheritance – Dean Edwards
* Defining classes and inheritance – Prototype JavaScript Framework

I won’t dig into details into these, but the above code would look something like this with John Resig’s implementation:

var Being = Class.extend({
	living : true,
	breathes : function () {
		return true;
	}
});

var Robert = Being.extend({
	blogs : true,
	getsBored : function () {
		return "You betcha";
	}
});

var me = new Robert();
me.getsBored(); // Returns "You betcha"
me.breathes(); // Returns true

Calling a parent’s method, super-style

If both the Being object and the Robert object were to have a getsBored method, you’d have the option to call the one belonging to the Being object in a very easy manner:

var Robert = Being.extend({
	blogs : true,
	getsBored : function () {
		// This is the syntax to call a super method
		this._super();
		return "You betcha";
	}
});

The way Robert (not the object) swings

I’d thought I’d share my take on the necessity of inheritance overall, and what syntax or solution to use when needed and justified.

Is there really a huge need for super calls?

First, I’d say that while it’s all nice and fine with super method calls, in my experience at least, there have been extremely few, if any, real live cases where I have felt a strong need, a mandatory path to go, with super relations. And apparently I’m not alone with this opinion. Douglas Crockford states:

I have been writing JavaScript for 8 years now, and I have never once found need to use an uber function. The super idea is fairly important in the classical pattern, but it appears to be unnecessary in the prototypal and functional patterns. I now see my early attempts to support the classical model in JavaScript as a mistake.

What syntax to use?

However, for those cases you need some sort of inheritance, what syntax should you use? Personally, I’d say that while I’m somewhat attracted to the class-syntax-like implementations, I prefer using the prototype approach for a number of reasons:

  • It’s native, i.e. no dependencies on other code.
  • Freedom of style and version, i.e. if it depended on other code, it would also depend on version of and implementor of that code.
  • Easy readability. I’m sure some of you would want to contradict me, but personally I think the prototype syntax is very easy to look at and understand.
  • Leaving the code to someone else. If you have used a native JavaScript solution, everyone knowing JavaScript will immediately understand it and be able to maintain or extend it. Not the same thing with custom code.

Is there a need for inheritance?

However, I would like to even take it one step further than above. It seems like a number of people overuse inheritance ever so often, ending up in spending most time overriding properties and methods in an object’s parent object, than actually using the parent object as an advantage.

For me, I tend to use the Yahoo JavaScript Module Pattern almost all of the time, and I’m quite happy with it! I ported DOMAssistant to it as well, and it’s a move I don’t regret.

The downside of the pattern is that you can’t (not easily, at least), create several instances of the same object. However, the need for this comes very seldom for me, and when it does, I prefer having used a good namespace I can extend (perhaps based on a custom method in the main object), instead of regular, or “true”, inheritance. A good example of my approach is How to create a plugin for DOMAssistant.

Embrace and join us!

I would humbly like to ask of you to embrace JavaScript. Don’t try to make it into Java, C or any other language. Learn to love it and its dynamic, loose and prototypical nature, and go with the flow. When you stop fighting is when you truly understand how wonderful JavaScript is!

http://robertnyman.com/2008/10/06/javascript-inheritance-how-and-why/

Web — No Comments
15
Feb 09

Ajax Kullanarak Dosya Yüklemek Mümkünmü?

Kısmen mümkün görünüyor. Ajax ile form post ederken "multipart/form-data" olarak göndersemde sadece dosyanın adını, gonderme methodunu  alabiliyorum. Yani formu gonder dediginizde sadece degiskenlerin isimleri gidiyor, dosya icerigi aktarilmiyor. Ancak aşağıdaki örnekte forma ek olarak dosya ile ilgili bilgileri manuel girebilirsek, sanırım dosya yüklemek mümkün olabilir. Ama bu method JPEG ve GIF tarzı dosyalarda degilde yine TEXT içerikli dökümanlarda işe yarar.

function sendAjaxRequest(){
var xrequest = new XMLHttpRequest();
var url = "htmeditor.php";
var boundaryString = ‘AaB03x’+ parseInt(Math.random()*9999999,10);
var boundary = ‘–’ + boundaryString;
var cr = "rn";

AddMensaje( boundary +cr);
AddMensaje("Content-Disposition: form-data; name="id""+cr+cr);
AddMensaje( IdTemplate + cr);
AddMensaje( boundary +cr);
AddMensaje("Content-Disposition: form-data; name="hayDatos""+cr+cr);
AddMensaje( "true" +cr);
AddMensaje( boundary +cr);
AddMensaje("Content-Disposition: form-data; name="content";
filename="datos.txt""+cr);
AddMensaje("Content-Type: text/plain"+cr+cr);
//AddMensaje( tinyMCE.getContent() + cr);//old version
AddMensaje( tinyMCE.activeEditor.getContent() + cr);
AddMensaje( boundary +cr);

xrequest.open("POST",url,false);
xrequest.setRequestHeader("Content-Type","multipart/form-data;
boundary="+boundaryString  );
xrequest.send(mensaje);

//alert(mensaje);
setTimeout("RecargarPagina()",200);
}

Graphic — No Comments
15
Feb 09

Iframe’in Boyutlariyla Oynamak

Aşağıdaki kod Iframe’inizin boyutlarını otomatik olarak şekillendirecektir.

<script type="text/javascript">
<!--
function autoResize(id){
var newheight;
var newwidth;

if(document.getElementById){
newheight=document.getElementById(id).contentWindow.document.body.scrollHeight;
newwidth=document.getElementById(id).contentWindow.document.body.scrollWidth;
}

document.getElementById(id).height= (newheight) + "px";
document.getElementById(id).width= (newwidth) + "px";
};
//-->
</script>

<iframe id="iframe1" name="iframe1" src="rastgele.html" width="200" height="200"
scrolling="no" onLoad="autoResize(this.id);" border="0" frameborder="0"
marginheight="0" marginwidth="0">

</iframe>

Kaynak: http://www.salihsdemir.net/iframe-otomatik-boyutlandirma/

Graphic — No Comments
11
Jan 09

JavaScript Object Orient Programing

Creating objects

The following function can be used as demonstrated to create an object of class myobject:

<code>function myobject() {
  this.containedValue = 0;
  this.othercontainedValue = 0;
  this.anothercontainedValue = 0;
}

var mything = new myobject();</code>

And there you go, mything is now an instance of class myobject. It will have the following properties, all
of which will be 0:

  • mything.containedValue
  • mything.othercontainedValue
  • mything.anothercontainedValue

You could also now write myobject.prototype.newContainedValue = someValue; and
all instances of class myobject will have the property newContainedValue with value someValue.

Creating an object with methods

Now I will give you an example showing you how to create methods for your objects. As an
example, I will create a circle as my object. The methods will be

nameOfCircle.retArea()
Returns the area of the circle (pi r2)
nameOfCircle.retCirc()
Returns the circumference of the circle (2 pi r)
nameOfCircle.mvBy(xDis,yDis)
Moves the circle by xDis in the x direction and yDis in the y direction Continue reading →
  • Blogroll

    • Fatih ACAR

© 2013. İpuçlarım } Serkan Konakcı. Powered by WordPress. Cleanr theme by WPShoppe.