Reputation: 523
I am developing an extension in which i need to generate URL of website differently.Here is the structure of my code i am using
<script>
var URL="";
function genURL(){
URL="xyz";
}
</script>
<body onload="genURL()">
<iframe id="if1" src="abc"></iframe>
</body>
<script>
document.getElementById("if1").src=URL;
</script>
above code is wrong as i cannot access element if1 in script code aboveits definitions and URL is not available in below script code.If we can access URL in Script code below , my problem will be solved. Or else is there a way to generate URL directly in iframe .
Thanks
Upvotes: 0
Views: 179
Reputation: 2303
You create a function that assigns a value to URL
, but you never call the function, so the code is created but not executed. There're a couple of ways to fix this. One is David's way. Another is to assign a value to URL outside of the function:
<script>
URL="xyz";
</script>
<iframe id="if1" src="abc"></iframe>
<script>
document.getElementById("if1").src=URL;
</script>
Upvotes: 1
Reputation: 129792
URL
is not set at the time you're setting the iframes src
. If you were to invoke genURL
first, then your code would work. It would be far better, however, to design your code as such:
function getURL() {
return "xyz";
};
...
document.getElementById('if1').src = getURL();
Upvotes: 5