Home > Technical & Creative Skills > Programming, Servers & Scripts

javascript using variable in function (4)


03-30-2016 09:53 PM #1 whats1thingnow (Member)
javascript using variable in function

been trying to get this to work for hours but still no go

so.. quick question for some coding experts

for this code, if i have the full url hardcoded, it works...

Code:
<script type="text/javascript">

$(function(){

$go({

"rightHtml": "<iframe src='http://fullurl.com/hardcoded-here-works' frameBorder=0 height=100% width=100% seamless=seamless></iframe>",


});
});

</script>
but if i have javascript variable defined already (var newurl = whatever; )

how do i pass that newurl and equate it to src?

i tried:

Code:
<script type="text/javascript">

$(function(){

$go({

"rightHtml": "<iframe src='newurl' frameBorder=0 height=100% width=100% seamless=seamless></iframe>",


});
});

</script>
and it failed...

tried other things like document.write and didn't work either.

thanks in advance for the help!


03-30-2016 10:02 PM #2 ysekse (Member)

Code:
<script type="text/javascript">

$(function(){

$go({

"rightHtml": "<iframe src='" + newurl + "' frameBorder=0 height=100% width=100% seamless=seamless></iframe>",


});
});

</script>
Not sure if this will work but I think it should. If you look closely the '" is a single quote followed by a double quote, on the left. And on the right, it is a double quote followed by a single quote.


03-30-2016 10:14 PM #3 whats1thingnow (Member)

you rock man!

i tried so many combinations to hack it out, but never tried that

i never understood the single quotes and double quotes...

can you explain what you did there?

why are there '+' on both sides again?

i have just been using a collected to swiped codes and never really understood it


03-30-2016 10:18 PM #4 ysekse (Member)

Assuming it worked, haha. Well, single quotes inside double quotes just go as characters inside a string, if that make sense.
Double quotes on the other hand are "stronger" or whatever you'd like to imagine it being. Like if you want to write say, inline js, like for example
onclick="function1('hi'); function2('hello');", and the functions take in strings you use single quotes for that cause a double quote would "break up" the string. + is just for concatenating strings. So looking at this:

"<iframe src='" + newurl + "' frameBorder=0 height=100% width=100% seamless=seamless></iframe>",
^String1 + string2+ String3
That is actually 3 strings. So you break it up where you need to insert your "newurl" variable, and concatenate them together.

Hope it made sense


Home > Technical & Creative Skills > Programming, Servers & Scripts