1

I want transfer value from one JavaScript to another JavaScript file when click on image where I have set attribute onclick. On the attribute I have set function where selected image is click, I will go to another PHP or HTML file, also it carry the value to another file.

Sorry for bad grammar.

last.setAttribute('onclick', 'imgClick(this)')
display.appendChild(last);

function imgClick(img) {
    location.replace("../view/packagedetails.php");
}
computercarguy
  • 2,173
  • 1
  • 13
  • 27
jumper
  • 105
  • 10

3 Answers3

1

What kind of data do you want to recover?

You can use query parameters:

function imgClick(img) {
    var src = img.getAttribute('src');
    location.replace("../view/packagedetails.php?img=" + src);
}

On packagedetails.php

<script>
   var imgUrl = document.URL.split('?img=')[1]
</script>

You can also send this data via POST to the PHP page, using a form.

Vitor Avanço
  • 1,015
  • 4
  • 8
0

You can transfer value from one javascript file to another in following ways:

1) You can set cookies from one file and read it from another

2) Using HTML web storage (see details here)

3) If the value is small you can just add it to the url as query parameter (here is an example)

4) You set the value in hidden input field or as an html attribute and read it from other javascript file

5) Define it in global scope for example window (see details here)

PS: never put any sensitive information in javascript file like secret tokens etc because its accessible by end user.

0

You can set local storage value in first js file and get it from another js file. this will not take lot of space, neither it will take any load on your code.

   localStorage.setItem("param_key", "value");//set
   localStorage.getItem("param_key");//get
Faseeh Haris
  • 669
  • 1
  • 11
  • 29