顯示具有 js 標籤的文章。 顯示所有文章
顯示具有 js 標籤的文章。 顯示所有文章

2008年12月4日 星期四

[JavaScript] Firefox 在 DHTML 解析上的問題

[JavaScript] Firefox 在 DHTML 解析上的問題

2008年12月3日 星期三

js, getMonth, getDate, getDay, date

JavaScript Date Object Reference

2008年11月27日 星期四

幹 竟然這樣寫

2008年11月13日 星期四

Destroy Without JavaScript

http://railscasts.com/episodes/77-destroy-without-javascript


<!-- projects/index.rhtml -->
<ul>
<% for project in @projects %>
<li>
<%=h project.name %>
<%= link_to_destroy "Destroy", project_path(project), confirm_destroy_project_path(project) %>
</li>
<% end %>
</ul>

<!-- projects/confirm_destroy.rhtml -->
<% form_for :project, :url => project_path(@project), :html => { :method => :delete } do |f| %>
<h2>Are you sure you want to destroy this project?</h2>
<p>
<%= submit_tag "Destroy" %>
or <%= link_to "cancel", projects_path %>
</p>
<% end %>


# routes.rb
map.resources :projects, :member => { :confirm_destroy => :get }

# projects_controller.rb
def confirm_destroy
@project = Project.find(params[:id])
end

# projects_helper.rb
def link_to_destroy(name, url, fallback_url)
link_to_function name, "confirm_destroy(this, '#{url}')", :href => fallback_url
end


/* application.js */
function confirm_destroy(element, action) {
if (confirm("Are you sure?")) {
var f = document.createElement('form');
f.style.display = 'none';
element.parentNode.appendChild(f);
f.method = 'POST';
f.action = action;
var m = document.createElement('input');
m.setAttribute('type', 'hidden');
m.setAttribute('name', '_method');
m.setAttribute('value', 'delete');
f.appendChild(m);
f.submit();
}
return false;
}

2008年10月16日 星期四

javascript, scope, function


<script type="text/javascript">
var global_function = null; <= 宣告在這裡才是global的
(function($){
function local_funtion() { <= 這裡只是 local的
}
global_funcion = function() { <= 指定變數 成function
}
})(jQuery);
</script>
<script type="text/javascript">
global_function(); <= global這裡才叫的到
</script>

2008年10月15日 星期三

substring, substr,js

String.substr(N1,N2) :從指定的位置(N1)截取指定長度(N2)的字串。
String.substring(N1,N2) :從指定的位置(N1)指定的位置(N2)的字串。

2008年8月19日 星期二

javascript event this

$('td.goal_del').bind('click', function(){goal_del(null,this );});
function goal_del(del_id,obj) {

2008年8月8日 星期五

JavaScript, prototype

http://blog.ericsk.org/archives/1089


var foo = {};
var bar = function() {}; // 也可以 function Bar() {};

if (foo instanceof Object) {
alert('Yes, foo is an Object instance');
}

if (bar instanceof Function) {
alert('Yes, bar is a Function instance');
}

var foo = {
x: 100,
y: 200,
f: function() {
....
}
};
如此一來你可以 foo.x, foo.y 或是 foo.f() 來操作

即使先用 new 生出 Function 的實體,之後再對該 Function 做 prototype 的新定義,被生出來的實體一樣會採用新的 prototype 定義。也就是對每一個實體而言,它綁住的是一個 prototype 而不是一個 class。

2008年8月4日 星期一

javascript get local time & rails

http://www.eie052.com/bcshow.php/t/java/p/0/d/30.html
http://www.builder.com.cn/2006/0123/227078.shtml
http://articles.techrepublic.com.com/5100-10878_11-6016329.html

[View]
$('.localtime').click(function(){
var t = new Date();
now = t.getTime();
window.location = '/alert/gettime?time='+now
});

getTime拿到的是1970年1月1日後到此當時時間之間的毫秒數。
但是記得要加上time zone offset
[Controller]
Time.at(params[:time].to_f/1000).to_s(:db)

為了不一直發出request

http://blog.roodo.com/jaceju/archives/6643083.html

這種用setTimeout的技巧 還蠻常用到的

var sending = null;

var _formSubmit = function () {
alert('Form submited!');
};

var _doAjaxPost = function () {
if (sending !== null) {
clearTimeout(sending);
sending = null;
}
sending = setTimeout(_formSubmit, 1000);
};

var plusQuantity = function () {
// ... 執行增加數量的動作 ...
_doAjaxPost();
return false;
};

var minusQuantity = function () {
// ... 執行減少數量的動作 ...
_doAjaxPost();
return false;
};

$(function () {
// 增加數量
$('a.plus').click(plusQuantity);

// 減少數量
$('a.minus').click(minusQuantity);
});

2008年8月1日 星期五

javascript reverse string , and add, for number

Q: add the ',' symbol in the number, like 1,234,567
reference:
http://www.javascripter.net/faq/converti.htm
http://www.ruby-forum.com/topic/79885
maxsize(x), maxlength(o)

http://www.bytemycode.com/snippets/snippet/400/
http://www.irt.org/script/1325.htm

>>> '123456'.split(/.../)
["", "", ""]
>>> '123456'.split(/(...)/)
["", "123", "", "456", ""]

ie split有問題, 所以只好自己做, 在下面


function fn_price(val) {
var val = val+'';

var a = val.split('').reverse();

var str = [];
for(var i = 0 ; i< a.length; i++){
if(i % 3 == 0 && i != 0) { str.push(',');}
str.push( a[i]);
}
return str.reverse().join('');
}

2008年7月24日 星期四

observe_field, with

http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001633

with 那邊的parameter還可以用prototype 去get 其他element的value

<%= observe_field :target_type_target_type_id,
:update => :target_list,
:on => "change",
:url => {:action => :update_target_list},
:with => "{target_type_id: value, target_cat_id: $('target_type_target_cat_id').value}"%>

update, replace_html

http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html#M001645
replace_html可以用partial 去取代

render :update do |page|
page.replace_html 'target_type_target_type_id',
:partial => 'target_type_select', :locals => {:types => target_type_content}
end

2008年7月17日 星期四

jquery, corner

http://malsup.com/jquery/corner/

<script type='text/javascript'>
$(function() {
$('.rounded').corner();
});
</script>

javascript history

http://www.techotopia.com/index.php/JavaScript_History_Object
http://www.exforsys.com/tutorials/javascript/javascript-history-object-properties-and-methods.html
<a href="javascript:history.go(-1)">back</a>

2008年6月10日 星期二

js onclick

<a class='bbt_pen' 'href='#' id="+v+" username='"+record.data.user_name+"' onclick='init_message(this);'><img src='images/mailbox.png'/></a>

onclick='init_message(this)'
要這樣用onclick

2008年6月9日 星期一

js split

http://www.w3schools.com/jsref/jsref_split.asp

html 是 string, 用空格當作分界轉成array, 取第二個
text = html.split(' ')[1];

form submit input

form submit時 哪些值才會被當作需要的參數傳到server勒?

只有用input tag 的才會被傳到server, 所以當有需要的參數就要放在input裡, 有些時候就需要自己建
$('div.hide-input').append("<input type=text value="+index+" name=item["+index+"][order] id=item["+index+"][order] />");

而且name id 兩個attribute缺一不可