Change src from an action-based image on the previous page [closed]

0

Hello, I need help doing the following because I can not do it at all: By clicking on a button (for example) I need to go to another page. Until then, okay. I need on this page that the image to be displayed is replaced by a specific link.

Example: Clicking on the jujuba button .. goes to the candy page and replace the src of the image id="doces_img" to src="jujuba.png"

preferably js. if you need more msm use a framework, jquery. I thought maybe create a variable and change the value of it hence when the page "candy" load, it will check the variable and replace the src to the desired. but if that's what it needs to be cached.

    
asked by anonymous 09.11.2016 / 03:01

1 answer

1

As commented out, a good solution is to use LocalStorage , at a glance: link

To preserve the code:

HTML

<button id="btnJujuba" type="button" src-img-dinamica="https://img.buzzfeed.com/buzzfeed-static/static/2015-07/29/10/enhanced/webdr15/enhanced-16875-1438181730-13.jpg">
  jujuba
</button>

<hr />

<img id="imgDinamica" src="http://galaxy.mobity.net/uploads/148/logo/1399898656.png"/>

JS

varbtnJujuba=document.querySelector('#btnJujuba');btnJujuba.addEventListener('click',function(){varsrcImgDinamica=this.getAttribute('src-img-dinamica');window.localStorage.setItem('srcImgDinamica',srcImgDinamica);loadImgDinamica();});functionloadImgDinamica(){varimgDinamica=document.querySelector('#imgDinamica');varsrcImgDinamica=window.localStorage.getItem('srcImgDinamica');if(srcImgDinamica){imgDinamica.setAttribute('src',srcImgDinamica);}}

TheloadImgDinamica()methodistosimulatethepageexchange,thatis,initsplaceyoudotheredirectandonthatpageyouexecutethecodethatisinsideit.

NotallbrowsersandversionssupportAPIofLocalStorage,see: link . To know if the browser supports LocalStorage simply test if it exists on object window :

if(window.localStorage) {
    // da suporte
} else {
    // não da suporte
}

For this you can make the solution using cookies, see link

To preserve the code:

HTML

<button id="btnJujuba" type="button" src-img-dinamica="https://img.buzzfeed.com/buzzfeed-static/static/2015-07/29/10/enhanced/webdr15/enhanced-16875-1438181730-13.jpg">
  jujuba
</button>

<hr />

<img id="imgDinamica" src="http://galaxy.mobity.net/uploads/148/logo/1399898656.png"/>

JS

varbtnJujuba=document.querySelector('#btnJujuba');btnJujuba.addEventListener('click',function(){varsrcImgDinamica=this.getAttribute('src-img-dinamica');document.cookie='srcImgDinamica='+srcImgDinamica;loadImgDinamica();});functionloadImgDinamica(){varimgDinamica=document.querySelector('#imgDinamica');varsrcImgDinamica=document.cookie.split('=')[1];if(srcImgDinamica){imgDinamica.setAttribute('src',srcImgDinamica);}}

Withcookies,thereisnodangerofthebrowserbeingabletosupportit,buttotreatit,itisalready"complicated" and "manual" because cookies do not work with "key and value" equal to LocalStorage so you have to search within String registered in the cookie to find the snippet of information you want. Maybe by transforming a javascript object into JSON and saving in cookie, then making parse of it to javascript object again improves access, I did not test it I just had that idea now.

But take a look at the cookie documentation: link

Another solution, as manual as using cookies is to use url parameter, I will not make a jsfiddle but it looks like this:

JS

var btnJujuba = document.querySelector('#btnJujuba');

btnJujuba.addEventListener('click', function() {
    var srcImgDinamica = this.getAttribute('src-img-dinamica');

    window.location.href='http://minhaurl?srcImgDinamica='+encodeURIComponent(srcImgDinamica);
});

// na outra página
function loadImgDinamica() {
    var imgDinamica = document.querySelector('#imgDinamica');

    var srcImgDinamica = getQueryParam().srcImgDinamica;

    if(srcImgDinamica) {
        imgDinamica.setAttribute('src', srcImgDinamica);
    }
}

function getQueryParam() {
    var queryString = {};
    window.location.search.substr(1).split('&').forEach(function (pair) {
      if (pair === '') return;
      var parts = pair.split('=');
      queryString[parts[0]] = decodeURIComponent(parts[1].replace(/\+/g, ' ')).replace(/\+/g, ' ');
    });
    return queryString;
}

This getQueryParam function is a function that transforms all Query Params of a url into a javascript object.

It can be said that the solution by URL is the least sophisticated, by LocalStorage most sophisticated and Cookie the intermediate between them.

    
09.11.2016 / 12:53