将MaxLength设置为动态<p> </p>

时间:2012-04-08 09:52:26

标签: javascript jquery

我有脚本,可以显示Bing搜索结果。我可以打电话给搜索结果网址如下:

'<p class="width"><a  href="' , imgResult.Url , '">',imgResult.DisplayUrl,'</a></p>' ,

问题在于,有时网址会长得像这样“马虎”:

http://www.art-wallpaper.net/Game-Wallpapers/Assassins-Creed-Brotherhood/imagepages/image3.htm

我想“隐藏”或“删除”网址的开头(http://www.)并从/Game...等处执行相同操作。这样我就可以获得'干净'和'短' url like:art-wallpaper.net我有一种(简单的)方法吗?

2 个答案:

答案 0 :(得分:0)

也许是这样的JavaScript函数。

function shorten(str)
{
   // Get rid of the protocol
   str = str.replace("http://", "");
   str = str.replace("https://", "");

   // Return the domain-part of the URL
   return str.split("/")[0];
}

在打印之前将您的URL传递给JavaScript函数。

<强>未测试

答案 1 :(得分:0)

url="http://www.art-wallpaper.net/Game-Wallpapers/Assassins-Creed-Brotherhood/imagepages/image3.htm";
    pathArray =url .split( '/' );
    host = pathArray[2];
    alert(host);// alert "www.art-wallpaper.net"

和,

    pathArray = "http://www.art-wallpaper.net/Game-Wallpapers/Assassins-Creed-Brotherhood/imagepages/image3.htm".split( '/' );
host = pathArray[2].substring(4);
alert(host);// alert "art-wallpaper.net"

相关问题