gpt4 book ai didi

c# - 动态javascript错误

转载 作者:行者123 更新时间:2023-11-30 12:52:01 25 4
gpt4 key购买 nike

我正在尝试从后面的代码创建一个动态 javascript,它将被添加到页面中,从而使一个小的 jwplayer 能够打开,到目前为止我的代码:

void playAudioFile(string path)
{
string script =
@" var div = document.getElementById('playerDiv');
var audio = document.createElement('audio');
audio.setAttribute('id', 'audioPlayer');
div.appendChild(audio);

jwplayer(""audioPlayer"").setup({
file: 'http://ps.staging.be/webservice/MP3DownloadHandler.ashx?id='" + path +
@"'&date=' + '<%=Encrypt.EncryptString(String.Format(""{0:dd/MM/yyyy}"",
DateTime.UtcNow))%>',
width: ""100%"",
height: ""30"",
type: 'mp3',
stretching: ""uniform"",
controlbar: 'bottom'
});
";

//Add the script to it's tags
HtmlGenericControl playScript = new HtmlGenericControl("script");
playScript.InnerHtml = script;
//Add it on his place
playerDiv.Controls.Add(playScript);

但这结果是“无法与 localhost:55323 取得联系,有人知道我犯了什么错误吗?+ 有更好的方法吗?目的是在有人点击我的元素时显示播放器网站,播放器路径会根据所选项目而改变。如果你想比较一些东西,我有点像 youtube 这样的系统。

最终解决方案


string date = Encrypt.EncryptString(String.Format("{0:dd/MM/yyyy}", DateTime.UtcNow));
string script =
@" var div = document.getElementById('ContentPlaceHolder1_playerDiv');
var audio = document.createElement('audio');
audio.setAttribute('id', 'audioPlayer');
div.appendChild(audio);

jwplayer(""audioPlayer"").setup({
file: 'http://ps.staging.be/webservice/MP3DownloadHandler.ashx?id=" + path + @"&date=" + date + @"',
width: '100%',
height: '30',
type: 'mp3',
stretching: 'uniform',
controlbar: 'bottom'
});
";

//Add the script to it's tags
HtmlGenericControl playScript = new HtmlGenericControl("script");
playScript.InnerHtml = script;
//Add it on his place
playerDiv.Controls.Add(playScript);

最佳答案

你应该使用 ClientScript.RegisterStartupScript这样做:

Page.ClientScript.RegisterStartupScript(this.GetType(), "FooKey", script, true);

您在脚本中有一个代码块,不确定为什么不在代码中构建该字符串,然后将其附加到脚本中:

string date = Encrypt.EncryptString(String.Format("{0:dd/MM/yyyy}", DateTime.UtcNow));
string script =
@" var div = document.getElementById('playerDiv');
var audio = document.createElement('audio');
audio.setAttribute('id', 'audioPlayer');
div.appendChild(audio);
jwplayer(""audioPlayer"").setup({
file: 'http://ps.staging.be/webservice/MP3DownloadHandler.ashx?id='" + path + @"'&date=" + date + ", width: '100%', height: '30',type: 'mp3', stretching: 'uniform',controlbar: 'bottom'});";

关于c# - 动态javascript错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20632435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com