Utilizing the variables from default.aspx.cs
within the script of the default.aspx
file has been achieved successfully with the following code:
public partial class default: System.Web.UI.Page
{
public string _file = string.Empty;
public string _img = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
_file = "~/videos/myVideo.mp4";
_img = "~/images/myImg.png";
}
}
The code in the default.aspx
file:
<html>
<body>
<div id="dv_video"></div>
</body>
</html>
<!-- jw Script -->
<script src="../jwplayer/jwplayer.js"></script>
<script type="text/javascript">
jwplayer("dv_video").setup({
file: '<%= _file %>',
image: '<%= _img %>',
height: 500,
width: 850
});
</script>
<!-- jw Script -->
To move the script code to an external JavaScript file extras.js
, the challenge is how to access the _file
and _img
variables in that JavaScript file. The code should look like:
jwplayer("dv_video").setup({
file: '<%= _file %>',
image: '<%= _img %>',
height: 500,
width: 850
});
Edited
The code used to pass parameters to the javascript file extras.js
:
<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/jwplayer/extras.js") %>">
_image: '<%= _img %>';
_file320: '<%= _file320 %>';
_file480: '<%= _file480 %>';
</script>
The code to utilize the parameters in the js file:
jwplayer("dv_video").setup({
image: window._image,
sources: [{
file: window._file480,
label: "480p HD",
"default": "false"
},
{
file: window._file320,
label: "360p SD",
"default": "true"
}],
height: 500,
width: 850
});