Vzupo
Vzupo

Reputation: 1468

Loading dynamic link in iframe

I have an iframe that I want to load the id from a variable. The issue is structuring the link so that it works. If I hardcode the actual number its fine, but if I add the variable to the end, it comes back as undefined. Any help would be great.

$(document).ready(){
var path='https://url.com?ou='+youvariable;
var youvariable=6606;
console.log(path);

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<iframe src="" width="600px" height="400px"></iframe>

Upvotes: 0

Views: 56

Answers (2)

Adison Masih
Adison Masih

Reputation: 824

You are accessing the variable before intialization, that's why it returns undefined.

try this:

$(document).ready(function () {
   var youvariable=6606;
   var path='https://url.com?ou='+youvariable;
   console.log(path);
})

just declare the variable before using it.

Upvotes: 1

Paul Martin
Paul Martin

Reputation: 469

Firstly, your document.ready() needs a function as an argument, like...

$(document).ready(() => {
  //your code here
});

secondly, the variable needs to be declared before it is used.

$(document).ready(() => {
  var youvariable=6606;
  var path='https://url.com?ou='+youvariable;
  console.log(path);
});

Output:

"https://url.com?ou=6606"

Upvotes: 0

Related Questions