2012-01-04 28 views
2

我有一個奇怪的錯誤,我很難過。超鏈接僞裝成不需要的親戚

在我的網頁我有

<a href="www.purple.com">Purple</a> 

,但點擊時它表現爲

<a href="http://mydomain.com/www.purple.com">Purple</a> 

然而,當我查看源代碼,這只是因爲它應該是。鏈接正在使用markdown語法在聊天應用程序中動態添加。因此,它不是一個簡單的解決方案,即向所有內容添加http://。有其他方法可以解決這個問題嗎?

編輯

我已經能夠使用JavaScript代替它周圍破解()在一個HTTP地址://在每一個環節的開始,但我還是想知道,如果有一個更輕鬆,更優雅的解決方案。

msg = msg.replace(/href="/ig, 'href="http://').replace(/http:\/\/http:\/\//ig, 'http://') 

回答

2

如果你不預置爲你的外部鏈接http://,他們將被視爲從您的域名本身鏈接 - 相對於在主叫文件所在的路徑,或在你的基地HREF指定的路徑。

0

假設您不能編輯或排除Markdown語法的故障(這是...顯然首先生成字符串),您可以使用jQuery在修復程序上添加創可貼。再次,這不是真正的首選...

$("a[href*='http://mydomain.com/www']").each(function(i, el){ 
    //this will iterate through all links with an href 
    //value that contains the above string 'http://mydomain.com/www' 

    var old_url = $(el).attr('href'); 
    var new_url = old_url.split('http://mydomain.com/')[1]; //split up the url, and correct the mistake 

    $(el).attr('href', new_url);//re-apply the href attribute to the element 
}); 
+0

是啊,鏈接是動態的,這樣就不會工作...我做了一個簡單的替換服務器端 – 2012-01-04 06:45:50