简体   繁体   中英

Why is Jquery prepending img src with window url?

So I have this simple code:

var openPopUp = function(_type, _src) {
    $(_type).bind('click', function(e) {
        e.preventDefault();
        $('#img_pop_up').bPopup({
            onOpen: function() {
                console.log(document.getElementById("jpg_pop_up")['src']);
                $("#jpg_pop_up").attr("src",_src);
                /*$("#jpg_pop_up").attr("src","http://freefever.com/stock/2013-abstract-hd-wallpaper.jpg");*/
            }
        });
    });
}

The line mentioned in the comment works just fine but when I pass a variable as _src it prepends the src with window.location url and then tries to get the image. Its like http://myserver.com/http://image_url/ Any idea why is that? I am passing the absolute url. When I do console.log(_src) it prints the complete url fine but when I print

console.log(document.getElementById("jpg_pop_up")['src']);

this prints my window location url! It is supposed to be null.

When I hardcode the path of the url it works fine but when I pass the path with a variable then it assumes that as a relative path.

Try this:

console.log(document.getElementById('jpg_pop_up').getAttribute("src"));

The important difference here is that the src attribute contains what you gave it, but the src property contains the fully-resolved URL.

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