.load DIV from another site and replacing URL after load

Asked

Viewed 36 times

0

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

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

Switching to:

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

Below 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 someone can help me, thank you.

  • Is the loading done in PHP or Javascript? Depending on the answer the solution changes.

  • javascript

1 answer

0


Well, considering it’s being read in Javascript, it’s possible to do this in several ways:

  1. 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 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");
    });
}
  1. Manipulating the DOM

Another option is to manipulate the DOM (Document Object Model). Considering that it is possible to use the Jquery library we can do so:

// 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 achieved by including a function in the $.load call:

$.load(" coloque a sua url aqui ", function() {
    // Acao a ser executada depois do carregamento
});
  • It worked, thank you very much man.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.