简体   繁体   中英

Storing Iframe src as a variable

In the options page of my Google chrome extension, the user defines a URL destination which saves to LocalStorage["dest"]

Can't get the code to work in the popup.html file though

<script type="text/javascript">
  url = localStorage["dest"]; 
  document.getElementById('site').src = url;
</script>

<iframe id="site"> </iframe>

What am I doing wrong? Basically I want the popup to load whatever site the user saves in the options page, in an Iframe...

The script tag is before the iframe tag. When you do document.getElementById('site') the iframe does not exist yet. You need to move the iframe tage before the script tag. In general, it's best to put all your scripts right before the closing body tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM