자바스크립트2020. 10. 29. 09:39

autolink-js

autolink-js is a small (about half a kilobyte), simple, and tested JavaScript tool that takes a string of text, finds URLs within it, and hyperlinks them.

Why bother releasing such a tiny little method?

I recently needed to find and hyperlink URLs in user-submitted text and was surprised to find that doing what seemed like such a simple task wasn't already a Solved Problem. Different regex solutions led to different unwanted side effects, and other utilities were far, far more complex and feature rich than I needed.

Basic Usage

autolink-js adds an autoLink() method to JavaScript's String prototype, so you can use it on any JavaScript string. Take a look at the tests, but essentially, after including either autolink.js or autolink-min.js to your page, it works like this:

// Input "This is a link to Google http://google.com".autoLink() // Output "This is a link to Google http://google.com'>http://google.com"

Additional Options

You can pass any additional HTML attributes to the anchor tag with a JavaScript object, like this:

// Input "This is a link to Google http://google.com".autoLink({ target: "_blank", rel: "nofollow", id: "1" }) // Output "This is a link to Google http://google.com' target='_blank' rel='nofollow' id='1'>http://google.com"

Callback

Callback option can be used to redefine how links will be rendered.

// Input "This is a link to image http://example.com/logo.png".autoLink({ callback: function(url) { return /\.(gif|png|jpe?g)$/i.test(url) ? '<img src="' + url + '">' : null; } }); // Output "This is a link to image

http://example.com/logo.png'>"

Example

Open example/example.html in your web browser and view the source for a simple but full-featured example of using with jQuery.

Running the tests

After cloning this repository, simply open test/suite.html in your web browser. The tests will run automatically.

 

 

출처 - https://github.com/bryanwoods/autolink-js

 

 

 

보통 많은 종류의 게시판에서 url형태나 email 형태에 자동 링크를 걸어주는 정규식을 사용하고 있습니다.. 좀전에 친구가 jsp로 게시판을 만드는 과정에서 자동링크를 할 수 있는 방법을 물어오길래 클라이언트에서 할 수 있는 방법을 생각해봤습니다. 자바는 1.4부터 RegExp 패키지를 사용할 수 있거든요...

 

생각해보니.. 서버측 부하는 적게 걸리면서 같은 효과를 낼 수 있을꺼 같아 팁으로 올립니다.

 

<html>

<body>

<script>

function autolink(id) {

        var container = document.getElementById(id);

        var doc = container.innerHTML;

        var regURL = new RegExp("(http|https|ftp|telnet|news|irc)://([-/.a-zA-Z0-9_~#%$?&=:200-377()]+)","gi");

        var regEmail = new RegExp("([xA1-xFEa-z0-9_-]+@[xA1-xFEa-z0-9-]+\.[a-z0-9-]+)","gi");

        container.innerHTML = doc.replace(regURL,"<a href='$1://$2' target='_blank'>$1://$2</a>").replace(regEmail,"<a href='mailto:$1'>$1</a>");

}

</script>

<div id="test">

폼체크 스크립트 lib.validate.js의 사용법은 http://maniacamp.com/examples/validate_howto.html 을 참조하세요

거친마루의 이메일은 comfuture@studyfriend.net 입니다.<br>

php스쿨의 주소는 http://www.phpschool.com 입니다

</div>

<script>autolink('test');</script>

</body>

</html>

 

게시판등에 적용할때는 내용을 출력할 td에 id를 부여해주고 autolink('id'); 형태로 사용하면 되겠지요 : )

미리보기는 링크#1을 참조하세요

 

출처 - http://www.phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&wr_id=14253&sca=&sfl=wr_subject&stx=%B8%B5%C5%A9&sop=and&page=4

 

 

 

 

function autoLink(content) {

    var regURL = new RegExp('(^|[^"])(http|https|ftp|telnet|news|irc)://([-/.a-zA-Z0-9_~#%$?&=:200-377()]+)', 'gi');

    var regURL2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;

    //var regEmail = new RegExp('([^:])([xA1-xFEa-z0-9_-]+@[xA1-xFEa-z0-9-]+\.[a-z0-9-]+\.[a-z0-9-]+)', 'gi');

    

    return content.replace(regURL, '$1<a class="autoLink" href="$2://$3" target="_blank">$2://$3</a>')

     .replace(regURL2, '$1<a class="autoLink" href="http://$2" target="_blank">$2</a>');

     //.replace(regEmail, '$1<a class="autoLink" href="mailto:$2">$2</a>');

}

 

 



출처: https://linuxism.ustd.ip.or.kr/1553?category=420916 [linuxism]

'자바스크립트' 카테고리의 다른 글

[javascript] 브라우져별 unload 이벤트 할당  (0) 2020.08.27
array deep copy  (0) 2020.06.22
IE 10 에서 form 객체 접근  (0) 2014.01.14
trim 구현  (0) 2013.09.25
구글 맵 API  (0) 2013.07.17
Posted by 선한열심
자바스크립트2020. 8. 27. 13:10

if (window.attachEvent) {

    /*IE and Opera*/

    window.attachEvent("onunload", function() {

        /* ......?!@# */

    });

} else if (document.addEventListener) {

    /*Chrome, FireFox*/

    window.onbeforeunload = function() {

        /* ......?!@# */

    };

    /*IE 6, Mobile Safari, Chrome Mobile*/

    window.addEventListener("unload", function() {

        /* ......?!@# */

    }, false);

} else {

    /*etc*/

    document.addEventListener("unload", function() {

        /* ......?!@# */

    }, false);

}

 

출 처 : http://plibet.blogspot.kr/2013/09/javascript-unload.html

'자바스크립트' 카테고리의 다른 글

javascript - url link 자동 인식(autoLink)  (0) 2020.10.29
array deep copy  (0) 2020.06.22
IE 10 에서 form 객체 접근  (0) 2014.01.14
trim 구현  (0) 2013.09.25
구글 맵 API  (0) 2013.07.17
Posted by 선한열심
자바스크립트2020. 6. 22. 13:21


function cloneObject(obj) {
var clone = {};
for(var i in obj) {
if(typeof(obj[i])=="object" &amp;&amp; obj[i] != null)
clone[i] = cloneObject(obj[i]);
else
clone[i] = obj[i];
}
return clone;
}

'자바스크립트' 카테고리의 다른 글

javascript - url link 자동 인식(autoLink)  (0) 2020.10.29
[javascript] 브라우져별 unload 이벤트 할당  (0) 2020.08.27
IE 10 에서 form 객체 접근  (0) 2014.01.14
trim 구현  (0) 2013.09.25
구글 맵 API  (0) 2013.07.17
Posted by 선한열심
자바스크립트2014. 1. 14. 18:35

<form name=f1><input type=text name=box></form>



=> f1.elements.box.value  버전에 상관없이 다 잘 된다 

'자바스크립트' 카테고리의 다른 글

[javascript] 브라우져별 unload 이벤트 할당  (0) 2020.08.27
array deep copy  (0) 2020.06.22
trim 구현  (0) 2013.09.25
구글 맵 API  (0) 2013.07.17
Tistory 블러그 복사하는 방법  (0) 2013.07.01
Posted by 선한열심
자바스크립트2013. 9. 25. 11:30

/*********************************************************************************************************
//  문자열 공백제거
//  s = "   장동건   ".trim()  -> s = "장동건"
**********************************************************************************************************/
String.prototype.trim = function()  {

    return this.replace(/(^\s*)|(\s*$)/g, "");
};

'자바스크립트' 카테고리의 다른 글

array deep copy  (0) 2020.06.22
IE 10 에서 form 객체 접근  (0) 2014.01.14
구글 맵 API  (0) 2013.07.17
Tistory 블러그 복사하는 방법  (0) 2013.07.01
[JS API] prototype.js ajax method  (0) 2013.06.20
Posted by 선한열심
자바스크립트2013. 7. 17. 18:02

'자바스크립트' 카테고리의 다른 글

IE 10 에서 form 객체 접근  (0) 2014.01.14
trim 구현  (0) 2013.09.25
Tistory 블러그 복사하는 방법  (0) 2013.07.01
[JS API] prototype.js ajax method  (0) 2013.06.20
[JS api] AjaxUpload 사용하기  (0) 2013.06.13
Posted by 선한열심
자바스크립트2013. 7. 1. 14:21

1. 메뉴에서 [파일] - [다른이름으로 저장]

2. 저장시 mht 가 html로 저장한다

3. 저장한 파일을 열어서  아래와 같은 부분을 삭제한다

 <SCRIPT type="text/javascript">
 document.oncontextmenu = new Function ('return false');
 document.ondragstart = new Function ('return false');
 document.onselectstart = new Function ('return false');
  document.body.style.MozUserSelect = 'none';
</SCRIPT>

4. html파일  브라우저로 확인 ( 드래그함 )

'자바스크립트' 카테고리의 다른 글

trim 구현  (0) 2013.09.25
구글 맵 API  (0) 2013.07.17
[JS API] prototype.js ajax method  (0) 2013.06.20
[JS api] AjaxUpload 사용하기  (0) 2013.06.13
자바스크립트에서 미디어쿼리 처럼 CSS 사용하기  (0) 2013.04.02
Posted by 선한열심
자바스크립트2013. 6. 20. 10:19

new Ajax.Request

Posted by 선한열심
자바스크립트2013. 6. 13. 10:09

JS : ajaxupload.js

[출처] AjaxUpload 사용하기|작성자 자루




window.onload  = function(){

  new AjaxUpload('#mainImg', {
   action : '/manager/appMainImgUpload.do?appId=${result.appId}',    //파일 업로드 처리 페이지 설정
   name : 'file',         //post방식으로 보낼 name 명 지정
   onComplete : function(file, response) {   //파일 업로드 성공하였을 때 수행될 함수
    var url = "이미지 업로드 폴더 경로 (웹경로 )" + response;
    $("#mainImg").attr("src",url);
   },
   onSubmit : function(file, ext) {    //파일 업로드 submit 전에 수행될 함수
    if(!(ext && /^(jpg|png|jpeg|gif)$/i.test(ext))) {
     alert("Error : invalid file extension");
     return false;
    }
   }
  });
 }

 

<script type="text/javascript" src="/common/Js/ajaxupload.js"></script> 

[출처] AjaxUpload 사용하기|작성자 자루

<script type="text/javascript" src="/common/Js/ajaxupload.js"></script> 

[출처] AjaxUpload 사용하기|작성자 자루[출처] AjaxUpload 사용하기|작성자 자루


Posted by 선한열심
자바스크립트2013. 4. 2. 16:19

간단히 가능한거만 확인

<link rel="stylesheet" type="text/css" href="css/main1.css" id=style1 />

<link rel="stylesheet" type="text/css" href="css/main2.css" id=style2 />

<link rel="stylesheet" type="text/css" href="css/main3.css" id=style3 />


<script type="text/javascript" src="js/jquery-1.8.1.min.js"></script>



// 스타일시트를 disalbed함

$("#style1").attr("disabled", "disabled"); 

$("#style2").attr("disabled", "disabled"); 

$("#style3").attr("disabled", "disabled"); 


function funTest(state){ 

$("#style1").attr("disabled", "disabled"); 

$("#style2").attr("disabled", "disabled"); 

        $("#style3").attr("disabled", "disabled"); 

 

$("#style"+ state).removeAttr("disabled");   // 스타일을 적용함 (즉 스타일시트 disabled한 것을 제거함 

}


</script>

<a href=javascript:funTest('1')>1111111111</a>

<a href=javascript:funTest('2')>2222222222</a>

<a href=javascript:funTest('3')>3333333333</a>

'자바스크립트' 카테고리의 다른 글

[JS API] prototype.js ajax method  (0) 2013.06.20
[JS api] AjaxUpload 사용하기  (0) 2013.06.13
웹표준, 웹접근성 및 Tree 구현  (0) 2013.04.02
new Person(), Person()의 차이  (0) 2013.02.19
addEventListener, attachEvent  (0) 2013.02.19
Posted by 선한열심