how to change an image src from another page

1

Hello, I have an input where it serves as a search bar. When entering a name and pressing enter, a function is activated and performs the following function:

function verificaValor(){
   var valor = $('#search').val();
   switch (valor) {   
      case "scripts":
        window.location.href = 'scripts.html';
        document.getElementById("imgDinamica").src="Slide1.png";
      break;

When you type scripts it goes to another page, but it does not change the image. How do I change the image of another page?

    
asked by anonymous 20.11.2016 / 04:50

1 answer

3

You can do it using only JavaScript if that's what you want. I would do so:

In place of your line:

window.location.href = 'scripts.html?imagem=Slide1';

And on page scripts.html , somewhere after where this imgDinamica appears:

/// copiado de http://stackoverflow.com/a/901144
function getParameterByName(name, url) {
    if (!url) {
      url = window.location.href;
    }
    name = name.replace(/[\[\]]/g, "\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

var imagem = getParameterByName('imagem');
if (imagem !== null) {
    document.getElementById("imgDinamica").src = imagem + ".png";
}
    
20.11.2016 / 12:45