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

2011年4月28日 星期四

GeoLocation sample code

var initialLocation = new Object();
    initialLocation.type = "CHT_Xinyi";
    initialLocation.x = "121.52435034130934";
    initialLocation.y = "25.03645913419541";
    initialLocation.h = "0";
    initialLocation.cityName ="";
    initialLocation.countryCode="";
    initialLocation.countryName = "TAIWAN";
   
function getUserGeoLocation(){
   
    var browserSupportFlag =  new Boolean(false);
    // Try W3C geolocation
    if(navigator.geolocation) {
        browserSupportFlag = true;
        //successCallback
        navigator.geolocation.getCurrentPosition(function(position) {
          
            initialLocation.type = "Location found using W3C standard";
            initialLocation.x = position.coords.longitude;
            initialLocation.y = position.coords.latitude;
            initialLocation.h = position.coords.altitude;;
            $("#type").html(initialLocation.type);
            $("#x").html(initialLocation.x);
            $("#y").html(initialLocation.y);
            $("#h").html(initialLocation.h);
            $("#cityName").html(initialLocation.cityName);
            $("#countryCode").html(initialLocation.countryCode);
                      
        //errorCallback
        }, function() {
            alert("navigator.geolocation exist...getCurrentPosition failed");          
        });
    // Try IP location IPInfoDB
    } else if (browserSupportFlag==false) {
   
        var service_url = "http://***********/geolocation/geolocation.aspx";
        $.ajax({
            url: service_url,
            type: 'GET',
            dataType: 'json',
           // async:false,
            error: function (xhr, textStatus) {
                alert(xhr.statusText);
            },
            success: function (data) {
                if (data == null) {
                    alert('NO RESULT');
                } else {
                    initialLocation.type = "Location found using IPInfoDB API";             
                    initialLocation.x = data.longitude
                    initialLocation.y = data.latitude;
                    initialLocation.h = "";
                    initialLocation.cityName = data.cityName;
                    initialLocation.countryCode = data.countryCode;
                    $("#type").html(initialLocation.type);
                    $("#x").html(initialLocation.x);
                    $("#y").html(initialLocation.y);
                    $("#h").html(initialLocation.h);
                    $("#cityName").html(initialLocation.cityName);
                    $("#countryCode").html(initialLocation.countryCode);
                }
            }
        });
    }
}

2010年11月16日 星期二

String.Format

//在程式中輸出字串的時候,常常會需要用到字串連接子和變數一起輸出
//使得code變得難以閱讀與維護
// String.Format可以幫我們簡化一些
// js 版本(C#有現成物件可使用)
String.format = function(){
    if(arguments.length == 0)
        return null;

    var str = arguments[0];
    for(var i=1;i < arguments.length;i++) {
       var re = new RegExp('\\{' + (i-1) + '\\}','gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
}
//ex
tmp = "
  • [捷運轉公車]
    由起點步行[{0}]公尺至捷運[{1}]站,經過[{2}]站後於捷運[{3}]站下車,隨後步行[{4}]公尺至公車[{5}]站轉乘公車[{6}],經過[{7}]站後於公車[{8}]站下車,隨後步行[{9}]公尺到達迄點"; value = String.format(tmp, C12, C13, C16, C17, C20, C22, C26, C27, C28, C32);
  • 2010年5月3日 星期一

    jQuery Draggable div

    jQuery 現在也有一些公司 host 檔案供大家直接使用

    so-called Content delivery network 像是Google

    工作緣故需要測試拖曳一個div中包transparent flash的功能

    < link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
    < script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">
    < script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js">
            < script>
        $(document).ready(function() {
           $("#draggable").draggable();
        });
     < / script>
    
    

    參考資料:

    UI/API/1.8/Draggable

    2010年2月3日 星期三

    Flex 呼叫/傳值 給 javascript

    // flex 傳值給js demo
    // jsFuncName--> 網頁中要接收參數的functon名稱 (js函式名稱)
    // param--> 要傳遞的參數.變數名稱 (參數內容)
    /*flex*/
    var jsFuncName:String="test";
    var param:String ="message";
    ExternalInterface.call(jsFuncName, param);
    

    /*javascript*/
    <script language="javascript">
    function test(param){
      alert(param);
    }
    < /script>
    

    //p.s. allowScriptAccess需打開

    javascript 資料型態、變數宣告 (var)

    變數型態

    Number 包含整數與浮點數
    Boolean true or false
    String 字串用單引號或雙引號包起來,字串有引號時則用單引號包起來
    Object myObj = new Object();
    Null no value and means nothing
    Undefined 變數被創造,卻沒有被assign值

    使用var宣告變數會是local variable,反之則整個page都可讀取

    // local variable
    var xxx1 = "abc";
    // global variable
    xxx2 = "abc"