.load DIV from another site and replacing URL after load

0

Hello, my problem is this. After loading a div from another domain, I want to change the URLs of these images of the loaded div to my domain. Ex:

<img class="InfoBarSmallElement" 
src="http://exemplo.com/img.png"/>

Switchingto:

<imgclass="InfoBarSmallElement" 
src="http://meudominio.com/img.png" />

Here is the script I'm using for .load:

<script>
$(function(){
var contentURI= 'http://www.exemplo.com/ .Div';
$('.Result').load('../system/getp.php?url='+ contentURI);
});
</script>

getp.php

<?php echo file_get_contents($_GET['url']); ?>

Thank you very much if anyone can help me, thank you.

    
asked by anonymous 29.04.2018 / 01:01

1 answer

0

Well, considering it's being read in Javascript, you can do this in several ways:

  • With regular expressions
  • This option tends to be more laborious, after all before you had a problem (change the src of the images) and now you will have two (change the src and use regular expressions). Fortunately Javascript treats ER's as first-class entities.

    function ajustaSrcER() {
        var expReg = /src=(("[^"]+")|('[^']+'))/igm;
    
        var texto = $(".Result").innerHTML;
    
        $(".Result").innerHTML = texto.replace(expReg, function(match, p1, p2, p3, offset, s) {
        return "src=" + p1.replace("http://exemplo.com","http://meudominio.com");
        });
    }
    
  • Manipulating the DOM
  • Another option is to manipulate the Document Object Model (DOM). Considering that it is possible to use the JQuery library we can do this:

    // funcao que busca todos os img's e altera seus src's
    function ajustarSrc() {
         $(".Result img").each(function() {
            var src = $(this).prop("src");
            $(this).prop("src", src.replace("http://exemplo.com","http://meudominio.com"));
         });
    }
    

    In both cases this should be performed immediately after loading. This can be obtained by including a function in the $ .load call:

    $.load(" coloque a sua url aqui ", function() {
        // Acao a ser executada depois do carregamento
    });
    
        
    29.04.2018 / 22:57