Jump to content
php.lv forumi
  • 0

Aizstāt URL ar linku HTML'ā


daGrevis

Question

Sveiki,

Ar JS neesmu nekādos draugos, šo skriptu atradu šajā - http://stackoverflow.com/questions/37684/replace-url-with-html-links-javascript - lapā. Pēc manām domām vajadzētu būt tā, ka lapā ir kāds links, piemēram "http://google.com/", un skripts to automātiski aizstāj ar "<a href="http://google.com/">http://google.com/</a>"... :) Tikai nestrādā. :(

http://dagrevis.net/problems/replace-links/

Link to comment
Share on other sites

Recommended Posts

  • 0

Memory leaki nav šī skripta problēma, bet gan JS implementāciju bugi. Korektai JS implementācijai būtu jāsaprot, ka DOM elements ir izdzēsts un attiecīgi ir jāizdzēš arī šim elementam piekarinātie eventi. Ja tā to nedara, tad tas ir bugs implementācijā.

Link to comment
Share on other sites

  • 0

briedi, tā jau bija domāts, ka tā funkcija ir priekš "text only" contenta. ja jau no paša sākumā tur būtu arī html (img,a:href), tad jau uri vietā uzreiz varētu ierakstīt tagu, un šāds replace vispār nebūtu vajadzīgs. bet vispārīgākam gadījumam te būs new version ;)

 

mefisto, piekrītu, ka vajag ņemt vērā memory leakus un uztaisīt ie specific js (līdzīgi kā conditional comments for css), taču šeit man tā nebija prioritāte, tāpēc ir "as is". starp citu firefoxā jau arī kādu brīdi pabrowsojot, ļoti pieaug memory & virtual memory: no sākotnējiem pāris desmitiem MB aiziet simtos MB. pat aizverot ciet visas lapas un atstājot tikai vienu defaulto, atmiņa neatbrīvojas, kamēr neaizver pašu browseri (task menedžerī to var labi redzēt). tā kā izmantotais memory apjoms uzpūšas jebkurā gadījumā un pat uz firefox (visticamāk tas notiek bez js palīdzības, turklāt vēl daudz apjomīgāk nekā js radītie memory leaki uz ie), tad es nemaz neiespringtu šajā sakarā kko labot uz ie (ja nu vienīgi kādā situācijā, kad tas tiešām būtu baigi nepieciešams)

 

šoreiz vismaz ātrāk izsaucu to uri replace: uzreiz pēc dom load, nevis tikai pēc window load

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">

function uri2lnk(s, aExc) {
var PN_LNK = /<a[^>]+>[\s\S]+?<\/a>/ig;
var PN_IMG = /<img[^>]+>/ig;
var PN_URL = /\b[a-z]+:\/\/([a-z0-9-._~!*()':/?&=+;,$@#%]+)/ig;
var PN_EMAIL = /\b[a-z0-9-._]+@[a-z0-9-._]+\.[a-z]{2,3}\b/ig;
var PN_DOMAIN = /\b[a-z0-9-._]+\.[a-z]{2,3}\b/ig;
var s2 = s, iId = 0, aM, a = [], i;

while (aM = PN_LNK.exec(s)) {
	a.push([iId, aM[0]]);
	s2 = s2.replace(aM[0], "%id:" + iId++ + "%");
}
while (aM = PN_IMG.exec(s)) {
	a.push([iId, aM[0]]);
	s2 = s2.replace(aM[0], "%id:" + iId++ + "%");
}
while (aM = PN_URL.exec(s)) {
	a.push([iId, '<a href="' + aM[0] + '">' + aM[1] + "</a>"]);
	s2 = s2.replace(aM[0], "%id:" + iId++ + "%");
}
while (aM = PN_EMAIL.exec(s)) {
	a.push([iId, '<a href="mailto:' + aM[0] + '">' + aM[0] + "</a>"]);
	s2 = s2.replace(aM[0], "%id:" + iId++ + "%");
}

s2 = s2.replace(PN_DOMAIN, '<a href="http://$&">$&</a>');
for (i = 0; i < a.length; i++) s2 = s2.replace("%id:" + a[i][0] + "%", a[i][1]);
if (aExc) for (i = 0; i < aExc.length; i++) s2 = s2.replace(aExc[i][0], aExc[i][1]);
return s2;
}

var gaExc = [
["IceFrog", '<a href="http://icefrog.com">IceFrog</a>'],
["SnowFrog", '<a href="http://snowfrog.com">SnowFrog</a>']
];
$(function() {$("#content").html(uri2lnk($("#content").html(), gaExc));});

</script>
</head><body>

<img src="http://google.com/images/logo_sm.gif" alt="" /><br /><a href="http://domain.lv">kkāds links</a><br />[email protected] lalala [email protected] lalala domain.lv lalala sub.domain.lv lalala domain2.lv lalala http://domain3.lv lalala asdf://domain4.lv lalala <span>http://domain5.lv/path/?p=123</span> lalala<br />un te ir arī google.lv, php.lv un ebay.com<br />un pat exceptional IceFrog, un hai būtu arī SnowFrog :P<br />vai tgd visi ir laimīgi? :D:D:D<br /><br />
<div id="content"><img src="http://google.com/images/logo_sm.gif" alt="" /><br /><a href="http://domain.lv">kkāds links</a><br />[email protected] lalala [email protected] lalala domain.lv lalala sub.domain.lv lalala domain2.lv lalala http://domain3.lv lalala asdf://domain4.lv lalala <span>http://domain5.lv/path/?p=123</span> lalala<br />un te ir arī google.lv, php.lv un ebay.com<br />un pat exceptional IceFrog, un hai būtu arī SnowFrog :P<br />vai tgd visi ir laimīgi? :D:D:D</div>

</body></html>

uri3_1260127909.png

Edited by 2easy
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...