如何在img src中添加'http:'以在javascript中获取正确的图片网址

时间:2012-06-05 10:02:10

标签: javascript wikipedia-api

我有一个javascript小部件,它通过MediaWiki API(http://en.wikipedia.org/w/index.php?title=Main_Page&action=render)从维基百科中获取文章。这是三星智能电视的应用程序。现在文本部分工作正常,但它不会显示任何图像,因为img src的结构如下:

  

“// en.wikipedia.org/wiki/File:example.jpg”

我通过使用PC模拟器来实现三星固件将其转换为此类型的完整网址:

  

“文件://localhost/en.wikipedia.org/wiki/File:example.jpg”

是否可以在src中添加“http:”,以便应用指向正确的链接,从而显示缩略图和全尺寸图像?

以下是js代码的相关部分:

UIContents.showImage = function(srcURLFromWikiPage, showHigherResolution) {
//    alert("UIContents.fillDescription()");


    var cutURL = srcURLFromWikiPage;

    //document.getElementById('UIContentsImgFrameiFrame').href = srcURL;
    //window.frames.UIContentsImgFrameiFrame.location.href = srcURL + "#file";

    //prepare link for full-size picture:
    //cutURL = cutURL.replace('thumb/','');
    //cutURL = cutURL.substring(0, cutURL.lastIndexOf('/'));

    //show preview thumb version
    //if (cutURL.match(/\.svg$/)) cutURL = srcURLFromWikiPage;
    alert('img src: ' + cutURL);
    //show preview thumb version
    var elemImg = document.getElementById('UIContentsImgFrameImage');

    document.getElementById('UIContentsImgFrame').style.display = 'block';
    //set image source
    if (showHigherResolution == true) elemImg.onload = 'UIContents.resizeImage()';
    elemImg.alt = 'loading...';
    elemImg.src = cutURL;

    elemImg.style.visibility = 'hidden';
    elemImg.style.visibility = 'visible';

    imageViewIsOpened = true;

    document.getElementById('UISearchField').style.visibility = 'hidden';
    document.getElementById('UISearchTextInput').style.visibility = 'hidden';

有什么建议吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

var cutURL = srcURLFromWikiPage.replace("file://localhost/","http://");