This should be super simple but I keep getting errors.
I have a JSON string that I need to add a js variable to, the code sample is below. Essentially I need to add frameVideo variable after the hash tag in the URL string.
var frameVideo = window.location.hash.substring(1);
jwplayer("sVid").setup({
'share-video': {
'code': '<embed src="http://www.website.com/test.php#"\'.frameVideo.\'" width="480" height="270" allowfullscreen="true" />'
},
});
What do I need to do differently?
You're using PHP concatenation instead of javascript style.
var frameVideo = window.location.hash.substring(1);
var JSON =
{
'code': '<embed src="http://www.website.com/test.php#"' + frameVideo + ' width="480" height="270" allowfullscreen="true" />'
};
Use +
instead of the .
to concatenate your string.
I'm guessing you are just coming over to javascript from PHP, given the coding style. In javascript, we use +
to concatinate, as opposed to the .
in PHP.
JSON strings are essentially javascript objects:
var obj = {
'share-video': {
'code': '<embed src="http://www.website.com/test.php#' + frameVideo + '" width="480" height="270" allowfullscreen="true" />'
}
}
You are also defining an index of an object outside of the context of an Object. 'variable':'something'
is a syntax error when outside of an object. Objects are encapsulated within {
and }
. The above code is semantically correct.
When debugging javascript, always check your console logs. They can help pinpoint exactly what is causing a problem. If you didn't know how to activate the console, it is accessed with F12
in most browsers, or by right-click->inspect element. I recommend Google Chrome for javascript debugging.
Use +
for concatenation.
No need to escape anything here either.
You have an extra double quote after the #
.
You want to use double quotes for key names in JSON (it probably wouldn't make a difference here, but a good habit regardless)
You have a trailing comma after your "share-video"
object.
var frameVideo = window.location.hash.substring(1);
jwplayer("sVid").setup({
"share-video": {
"code": '<embed src="http://www.website.com/test.php#' + frameVideo + '" width="480" height="270" allowfullscreen="true" />'
}
});
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.