如何在此脚本可点击链接中创建引用?

时间:2012-04-15 23:03:11

标签: javascript

我有来自codelifter的这个脚本,效果很好。 我想如果下面的引文包含一个网址,那么它将变为可点击。 任何帮助都会很棒。 感谢

<script language="JavaScript">
// ==============================================
// Copyright 2004 by CodeLifter.com
// Free for all; but please leave in this header.
// ==============================================

var Quotation=new Array() // do not change this!

// Set up the quotations to be shown, below.
// To add more quotations, continue with the
// pattern, adding to the array.  Remember
// to increment the Quotation[x] index!

Quotation[0] = "http://www.youtube.com/watch?v=zH6U5y086hw";
Quotation[1] = "Sanity is a golden apple with no shoelaces.";
Quotation[2] = "Repent! The end is coming, $9.95 at Amazon.";
Quotation[3] = "Honesty blurts where deception sneezes.";
Quotation[4] = "Pastry satisfies where art is unavailable.";
Quotation[5] = "Delete not, lest you, too, be deleted.";
Quotation[6] = "O! Youth! What a pain in the backside.";
Quotation[7] = "Wishes are like goldfish with propellors.";
Quotation[8] = "Love the river's \"beauty\", but live on a hill.";
Quotation[9] = "Invention is the mother of too many useless toys.";

// ======================================
// Do not change anything below this line
// ======================================
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>

2 个答案:

答案 0 :(得分:1)

这就是我要做的事情:

  1. 假设引用以http(s)://
  2. 开头
  3. 在ShowQuotation中:使用正则表达式来识别此类字符串。可能会不必要地复杂化。检查长度为7的子字符串是http://还是长度为8的子字符串为https://,假设其余部分都是URL的一部分
  4. 将其设为链接

答案 1 :(得分:1)

function showQuotation(){
    if(Quotation[whichQuotation].substr(0,7)=='http://' || Quotation[whichQuotation].substr(0,8)=='https://')
    document.write('<a href="'+Quotation[whichQuotation]+'">'+Quotation[whichQuotation]+'</a>');
    else
    document.write(Quotation[whichQuotation]);
}