How to pass hash into url from page to iframe?

Member

by orpha , in category: HTML & CSS , 4 months ago

How to pass hash into url from page to iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lizzie , 4 months ago

@orpha 

To pass a hash value from a webpage to an iframe, you can use JavaScript to set the URL of the iframe with the hash value from the parent page.


Here's an example code snippet to achieve this:


In the parent page, you can set the hash value in the URL of the iframe using JavaScript:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<body>
<p>Parent Page</p>

<iframe id="myIframe" src="iframe.html"></iframe>

<script>
  var hashValue = "examplehash"; // Set the hash value you want to pass
  
  // Set the URL of the iframe with the hash value
  var iframe = document.getElementById('myIframe');
  iframe.src = iframe.src + '#' + hashValue;
</script>

</body>
</html>


In the iframe page (iframe.html), you can access the hash value passed from the parent page:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<!DOCTYPE html>
<html>
<body>
<p>IFrame Page</p>

<script>
  var hashValue = window.location.hash.substring(1); // Get the hash value
  
  console.log(hashValue); // Output the hash value
  
</script>

</body>
</html>


With this setup, the hash value set in the parent page will be passed to the iframe page and can be accessed in the iframe using JavaScript.