Translate

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

2014年3月11日 星期二

MVC Javascript alert return


It's a simple example :

public ActionResult AlertMsg(string id)
{
  string url = Url.Content("~/Area/Control/AlertMsg/" + id);
  return Content(string.Format("<script>alert('ok'); window.location.href='{0}'",url)</script>","text/html");
}

2013年7月24日 星期三

Enter鍵按下後自動執行事件

兩種寫法:

1.javascript dopostback

<div id="divPanel">
    <asp:TextBox ID="txbContent"  runat="server" Width="60%" />
    <asp:LinkButton ID="butAdd" runat="server" Text="新增" OnClick="butAdd_Click" />
</div>

    <script type="text/javascript">
        $(document).keypress(function (e) {
            if ($('#divPanel').is(":visible") && e.which == 13) {
                __doPostBack('<%=butAdd.UniqueID%>', "");
            }
        });
    </script>

UniqueID詳細說明:

2.panel

用panel將要的元件包起來
並設定好defaultbutton屬性為Enter鍵後執行的buttonID

ex:
                <asp:Panel ID="pAdd" runat="server" DefaultButton="butAdd">
                        <asp:TextBox ID="txbContent"  runat="server" Width="60%" />
                        <asp:LinkButton ID="butAdd" runat="server" Text="新增" OnClick="butAdd_Click" />
                    </div>
                </asp:Panel>

參考:

2012年11月21日 星期三

TextBox DateTime應用-取得日期區間

ref→http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript
ref→http://netbeansboy.org/2011/09/02/javascript-isdate-function-for-uk/

ref→http://taiwantc.com/js/js_tut_a0.htm
ref→http://www.ezineasp.net/post/Javascript-Convert-String-to-Date.aspx
ref→http://api.jquery.com/val/

ref→http://blog.programmingsolution.net/javascript/casting-jquery-object-to-javascript-object-and-javascript-object-to-jquery-object/


<script type="text/javascript">

        var INTERDAY = 2;
        $("#<%=TimeStart.ClientID%>").bind('change', function (e) {
            var endText = $("#<%=TimeEnd.ClientID%>");
            var edate = GetTextDate(endText);
            var date = GetTextDate($(this));
            if (CheckInInterDate($(this), edate, INTERDAY) == false || edate < date) endText.val(dateToYMD(date, INTERDAY));
        });

        $("#<%=TimeEnd.ClientID%>").bind('change', function (e) {
            var startText = $("#<%=TimeStart.ClientID%>");
            var sdate = GetTextDate(startText);
            var date = GetTextDate($(this));
            if (CheckInInterDate($(this), sdate, INTERDAY) == false || sdate > date) startText.val(dateToYMD(date, -1 * INTERDAY));
        });

        function GetTextDate(obj) {
            if (isDate(obj.val()) == false) obj.val(dateToYMD(new Date(), 0));
            return StringtoDate(obj.val());
        }

        function CheckInInterDate(obj, date, interday) {
            var checkbefore = StringtoDate(obj.val());
            var checkafter = StringtoDate(obj.val());
            checkbefore.setDate(checkbefore.getDate() - interday);
            checkafter.setDate(checkafter.getDate() + interday);
            return checkbefore <= date && date <= checkafter;
        }

        function dateToYMD(date, interday) {
            date.setDate(date.getDate() + interday);
            var d = date.getDate();
            var m = date.getMonth() + 1;
            var y = date.getFullYear();
            return '' + y +'-'+ (m<=9?'0'+m:m) +'-'+ (d<=9?'0'+d:d);
        }

        function StringtoDate(dateStr) {
            var datePat = /^(\d{4})(-)(\d{1,2})(-)(\d{1,2})$/;
            var matchArray = dateStr.match(datePat); // is the format ok?
            if (matchArray == null) return new Date();
            return new Date(matchArray[1] + '/' + matchArray[3] + '/' + matchArray[5])
        }

        function isDate(dateStr) {
            var datePat = /^(\d{4})(-)(\d{1,2})(-)(\d{1,2})$/;
            var matchArray = dateStr.match(datePat); // is the format ok?
            if (matchArray == null) return false;

            day = matchArray[5];
            if (day < 1 || day > 31) return false;

            month = matchArray[3];
            if (month < 1 || month > 12) return false;
            if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) return false;

            year = matchArray[1];
            if (month == 2) { // check for february 29th
                var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
                if (day > 29 || (day == 29 && !isleap)) return false;
            }
            return true; // date is valid
        }
</script>

補充:2012/12/4
發現在IE9版本以下會略有問題:微修及調整之後…
this的用法真的要多加注意/   \



2012年5月28日 星期一

移來移去(mouseover/mouseout)顯示tip



只要將滑鼠放在LinkButton上就會顯示訊息
移開就不顯示
因為ToolTip要等待,故有時會採此方式顯示

<asp:LinkButton ID="btntest" runat="server"  class=" mousebtn "/>
<asp:Label ID="Label1" runat="server" Text="" class="note"></asp:Label>

 <script type="text/javascript">


        $(document).ready(function () {
           $(".mousebtn").live('mouseover', function () { $(".note").text("顯示姓名"); }).live('mouseout', function () { $(".note").text(""); });
        })
 </script>

2012年4月24日 星期二

confirm使用resource資源檔文字

找了幾種方法,不過通常都會出現錯誤,目前使用比較好的一種方法
就是在script加上function再使用
不過這樣一來就不是很便利了,苦惱,但也解決了目前的問題


 <asp:LinkButton ID="butDel" runat="server" ToolTip="<%$ Resources:admin. basedate , 刪除%>"  OnClientClick="return checkdel();"/>


 <script type="text/javascript">      
function checkdel()
 { var msg = "<%= Resources.basedate.將刪除所有基本資料 %>"; return confirm(msg); }
</script>

總的來說就是先將資源檔放入變數,再傳回來XD

更次更新→ return confirm('<%= Resources.basedate.將刪除所有基本資料 %>');

直接使用上面的語法效果比較好...

2012年4月12日 星期四

GridView Eval為變數新開視窗

如下列代碼

<asp:GridView ID="GridView">
<a onclick="link_web('<%# Eval("ID") %>')">連結書藉</a>
 </asp:GridView>

<script type="text/javascript">
        function  link_web (obj) {
           alert(obj);
            //TODO Link
        }
 </script>


用<a></a>將要連結的包起來 ,就可以順利連結

2012年3月30日 星期五

aspx頁面上使用資源字串

當要在aspx頁面上使用資源字串的方法

通常是用在不需要放在控制項目內的字串


<%=Resources.資源檔名.key值%>




通常是放在控制元件的寫法…

 <asp:ListItem Value="1" Text="<%$ Resources: 資源檔名 ,  key值 %>"/>

2012年3月23日 星期五

javascript inputtext擋住特定字元

     當你希望使用輸入文字及貼上一段文字,能避開某些特殊符號,或許可以參考
這裡範例使用的是 ? 及 & 可以改成任何你想擋的
它們的相對應key值是55及191…

 <script type="text/javascript">
  function RemoveText(obj) {
            var str = obj.val();
            str = String(str).replace("?", "").replace("&", "");
            obj.val(str);
        }     
       $("input[type='text']").bind('paste keydown keyup', function (e) {
            var el = $(this);
            setTimeout(function () { RemoveText($(el)); }, 1);
            if (e.type == 'keyup') {
                eKey = e && (e.which || e.keyCode)
                if (e.shiftKey && (eKey == 55|| eKey == 191)) {
                    alert("禁止輸入特殊符號");
                }
            }
        });
</script>

ascii→http://www.asciitable.com/

ref→http://api.jquery.com/bind/
ref→http://www.w3schools.com/jsref/event_onchange.asp
ref→http://stackoverflow.com/questions/3781142/jquery-or-javascript-how-determine-if-shift-key-being-pressed-while-clicking-an
ref→http://blog.longwin.com.tw/2007/08/javascript_match_replace_method_2007/


另外一開始也想用regex來判定,找到的一些方便網站也註記在此
ref→http://blog.roodo.com/rocksaying/archives/2670695.html
ref→http://www.jb51.net/article/23196.htm
ref→http://blog.miniasp.com/post/2010/04/27/How-to-filter-special-characters-using-NET-Regex.aspx
ref→http://joy0626.pixnet.net/blog/post/25259201
ref→http://www.blueshop.com.tw/board/show.asp?subcde=BRD20050715144115ZUM
ref→http://blog.xuite.net/ron825/enjoy/41475369-javascript%E5%AD%97%E4%B8%B2right%E5%8A%9F%E8%83%BD%E8%88%87left%E5%8A%9F%E8%83%BD


補充→之後發生了一點小問題,是有關輸入法,冏"如果是注音輸入法可能無法順利填入…
另…
加一個方法如下



      function RemoveText(obj) {
          var str = obj.val();
          if (str != "") {
              var aspecil= [" ? ", " & "]; //禁止輸入的符號 ? , &
              var flag = false;
              var j = aSign.length;

              while (j--) {
                  while (str.indexOf( aspecil [j]) >= 0) {
                      flag = true;
                      str = String(str).replace( aspecil [j], "");
                  }
              }

              if (flag) {
                  alert("<%=Resources.Contacts.禁止輸入特殊符號 %>");
              }
              obj.val(str);
          }
      }

      $("input[type='text']").bind('blur paste', function (e) {
          var el = $(this);
          setTimeout(function () { RemoveText($(el)); }, 1);
      });

2012年3月22日 星期四

javascript 剪貼本取得資料

剪貼本取得資料,其實語法,只有很短的一行
indow.clipboardData.getData("Text")

但是悲劇在於只有IE才能使用冏”

最後總算是很歡樂的試出大部份版本都有的,如下…


elem.bind('paste', function (e) {

                var text = "";

                if (window.clipboardData && window.clipboardData.getData("Text")) { //IE
                    text = window.clipboardData.getData("Text");
                }
                else if (e.clipboardData && e.clipboardData.getData) // Standard
                {
                    text = e.clipboardData.getData('text/plain');
                }
                else if (e.originalEvent.clipboardData &&
                    e.originalEvent.clipboardData.getData) // jQuery
                {
                    text = e.originalEvent.clipboardData.getData('text/plain');
                }

                alert(text);
            });

但是後來ff讓我無言了,因為他的預設都是空值

後來…

也總算找到了可以通用的…


            elem.bind('paste', function (e) {
                var el = $(this);
                setTimeout(function () {
                    var text = $(el).val();
                    alert(text);
                }, 100);
            });


ref→http://stackoverflow.com/questions/686995/jquery-catch-paste-input

==後面是補充參考
ref→http://jsfiddle.net/SLaks/nt4SD/
ref→http://stackoverflow.com/questions/8655604/attach-a-handler-for-all-event-types
ref→http://www.javascriptkit.com/javatutors/navigator.shtml
ref→http://topic.csdn.net/t/20020612/19/798745.html
ref→http://php.net/manual/en/function.get-browser.php

2012年3月21日 星期三

javascript 好用的分享連結

http://help.dottoro.com/ljuimtmq.php

javascript reload出現詢問視窗

ref→http://www.cnblogs.com/huobazi/archive/2004/04/06/RefreshOrReloadParentWindowWithoutConfirm.html

發現在IE9或Firfox使用opener.location.reload()
當有要從子網頁取得資訊給原本開啟的網頁用時,都會跳出擾人的詢問視窗
目前只要將此一段採用→ window.opener.location.href=window.opener.location.href;
採取重新定位的方式,就可以跟擾人的訊息saygoodbye了

2012年3月13日 星期二

離開頁面顯示訊息,當有TreeView的忽略方法

因為IE在點選TreeNode的時後,也會判定頁離開,故採用以下方法即可


<span onclick="window.document.body.onbeforeunload=null;return true;">
<asp:TreeView ID="TreeView1" runat="server">....</asp:TreeView>
</span>



 <asp:LinkButton ID="btn1" runat="server" onclick="Clickbtn" OnClientClick="window.document.body.onbeforeunload=null;return true;"></asp:LinkButton>



<script type="text/javascript">

window.document.body.onbeforeunload = function () { return '請問您確定不儲存離開嗎?'; }
</script>


補充:如果使用firefox請愛用→opener.window.parent.onbeforeunload = null;

java script,取得資源字串

<script type="text/javascript">

  var msg = "<%=Resources.msg.訊息 %>"
 alert(msg);

</script>

以此取得訊息即可

2012年2月29日 星期三

一起学 Microsoft AJAX Library

http://www.cftea.com/c/2009/05/AK25S4IJIEJ9D319.asp

存取被拒 ,行:5959 解決方式(未證實

ref:http://www.dotblogs.com.tw/jimmyyu/archive/2009/04/21/8116.aspx

跟上面的遇到相同問題
當iframe使用到了scriptManager
然後就存取拒絕,偏偏是舊版本的…
目前實做了一下,遇到了sys/type未定義
因為我不僅僅只有iframe頁面使用,連main頁面也使用
最後…
只有忍痛先將iframe頁面先拿掉了…
目前先將連結記錄在此,有空在拿來實測一次

2012年2月21日 星期二

KB927917錯誤解決方法

出现此问题的原因子容器 HTML 元素包含试图修改子容器的父容器元素的脚本。 脚本试图使用innerHTML  方法或  appendChild  方法修改父容器元素。例如如果 DIV 元素是在 BODY 元素中的子容器,并在 DIV 元素中的一个 SCRIPT 块尝试修改 DIV 元素的父容器的BODY 元素,可能会出现此问题




ref→http://www.nczonline.net/blog/2008/03/17/the-dreaded-operation-aborted-error/

  • Move the script element so that it’s a direct child of body.
  • Use insertBefore() to insert the div at the beginning of body instead of the end.
  • Wait until the page is loaded before attempting to manipulate document.body.


我採用了
$(document).ready(function()
{
//TODO
})

這個方式的確是解決XD(大部份都是發生在IE8直接顯示錯誤並無法連結)



2012年2月15日 星期三

javascript心得,子元件的顯示與消失測試

 <div id="1">
  <div id="1_1" style="display: block;"> 我是子項目一</div>
  <div id="1_2" style="display: block;"> 我是子項目二</div>
 </div>

  <input id="checkGetFirstData"  type="checkbox" onclick="CheckGetData(this)"/>

<script>

function CheckGetData(obj)
{
var tree = document.getElementById("1");

var treendes =  tree.childNodes;

for(i=0; i <treendes.length; i++)
{
   if(treendes[i].nodeType==1 && treendes[i].tagName)
   {
    if(obj.checked)
{
tree.childNodes[i].style.display="none";
}
else
{
tree.childNodes[i].style.display="";
//tree.childNodes[1].style.display="block";
}
   }
}
}
</script>



//下面就可以測試用TreeView模擬點擊打開及關閉



function expand(obj,closestr,openstr,treeid) {
    var treeobj = document.getElementById(treeid);
    var check = (obj.value.indexOf('展開') != -1) ? "Expand" : "Collapse";
    obj.value = (obj.value != openstr) ? openstr : closestr;
    for (var i = 0; i < treeobj.childNodes.length; i++) {
        if (treeobj.childNodes[i] != null && treeobj.childNodes[i].id) {
            TreeNodeExpand(document.getElementById(treeobj.childNodes[i].id), check);
        }
    }
}

//展開或折曡子節點
function TreeNodeExpand(elm, check) {
    for (var i = 0; i <= elm.childNodes.length; i++) {
        var child = elm.childNodes[i];
        if (child != null) {
            arguments.callee(child, check);
            if (child.id != null && child.id != "" && child.getAttribute('href') && child.firstChild.tagName == 'IMG') {
                if (child.firstChild.getAttribute('alt').indexOf(check) != -1) {
                    eval(child.getAttribute('href').replace('javascript:', ''));
                    //window.location = child.getAttribute('href');
                }
            }
        }
    }
}

Ps另外有可能FF會多判定→if (nodes[i].nodeType == 1 && nodes[i].tagName )子群組加上這個就可以看到正確

javascript心得,取得設定輸入元件的Value

ref→http://perishablepress.com/
ref→http://hi.baidu.com/webworker/blog/item/1595fbded544f55395ee37f3.html
ref→http://www.w3schools.com/js/js_obj_array.asp
ref→http://stackoverflow.com/questions/237104/array-containsobj-in-javascript
ref→http://www.cnblogs.com/lianqianxue/archive/2011/05/17/2048856.html
ref→http://hi.baidu.com/yxhua240/blog/item/f7b186135804f2ddf7039eea.html
ref→http://www.dotblogs.com.tw/eason.yen/archive/2011/02/17/21419.aspx
ref→http://www.cnblogs.com/yond/
ref→http://webcenter.hit.edu.cn/articles/2009/05-16/05070500.htm

當只要設定輸入欄位的值,只需要這樣做即可
連Firefox,IE都可支援了


<body>
  <input type="text" id="text1"  onkeyup=this.setAttribute('value',this.value) class="test" value="123" size="28">
  <input type="text" id="text2"  class="test" value="456" size="28">
  <input id="checkGetFirstData"  type="checkbox" onclick="CheckGetData()"/>
</body>



function CheckGetData()
{
var obj = document.getElementById("checkGetFirstData");
var objtext = document.getElementById("text1");
if(obj.checked)
{
objtext.value = "";
//objtext.setAttribute('value',this.value); //Firefox無法使用
//alert("check!")
}
else
{
objtext.value = "bbb";
//alert("no check!")
}
}

2012年2月9日 星期四

跳出訊息並轉頁面

ref→http://stackoverflow.com/questions/2345807/window-location-does-not-work-on-chrome-browser
ref→http://msdn.microsoft.com/zh-tw/library/system.web.ui.page.registerclientscriptblock(v=vs.80).aspx


Page.RegisterClientScriptBlock("alert", "<script>alert('請先登入會員!');</script>");
Page.RegisterClientScriptBlock("clientScript", "<script>window.location='index.aspx';window.location.href='index.aspx';</script>");

2011年12月1日 星期四

文字連續跑馬燈


在網路上找了許久,本來想以jquery製作,不過發現比較像「段落式」的跑馬燈
一下就跑一段而如果有原本的html語法的跑馬燈的話,則是會在結束後留下一大段空白

需求是:【文字/連續/尾接頭】

最後發現了一篇還不錯

可以參考如下

<DIV id=all style="OVERFLOW: hidden; WIDTH: 210px; HEIGHT: 120px">
<DIV id=m1>
<TABLE height=15 cellSpacing=0 cellPadding=0 width=210 align=center border=0>
<TBODY>
<TR>
<TD>
一段話<BR>
二段話<BR>
三段話<BR>
</TD>
</TR>
</TBODY>
</TABLE>
</DIV>
<DIV id=m2></DIV>
</DIV>

<SCRIPT type=text/javascript>
var speed=45
m2.innerHTML=m1.innerHTML
function Marquee(){
if(m2.offsetTop-gg.scrollTop<=0)
all.scrollTop-=m1.offsetHeight
else{
all.scrollTop++
}
}
var MyMar=setInterval(Marquee,speed)
all.onmouseover=function() {clearInterval(MyMar)}
all.onmouseout=function() {MyMar=setInterval(Marquee,speed)}
</SCRIPT>

補充一點,如果FireFox看不到效果的話,最好增加一些語法

var m2= document.getElementById('m2');
var m1= document.getElementById('m1');
var all= document.getElementById('all');

引用:

參考:
http://blog.hsin.tw/2008/javascript-vertical-carousel/