简体   繁体   中英

How to add 'http:' to img src to get correct image url in javascript

I have a javascript widget which fetches articles from Wikipedia through MediaWiki API (http://en.wikipedia.org/w/index.php?title=Main_Page&action=render). It's an app for Samsung Smart TVs. Now the text part works fine, but it won't display any images because the img src is structured like this:

"//en.wikipedia.org/wiki/File:example.jpg"

and I've come to realize by using a PC emulator that the Samsung firmware converts it to this type of full url:

"file://localhost/en.wikipedia.org/wiki/File:example.jpg"

Is it possible to add "http:" to the src so that the app points to the correct links hence displaying both thumbnails and full-size images?

Here's the relevant part of the js code:

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';

Any suggestions? Thanks in advance.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM