gpt4 book ai didi

asp.net - js在页面内部工作但不在代码后面

转载 作者:行者123 更新时间:2023-11-29 10:00:08 27 4
gpt4 key购买 nike

我将这个函数放在一个 MasterPage 中,它显示了一个 mp3 播放器:

<script type="text/javascript">
$(document).ready(function() {
var stageW = 500;
var stageH = 216;
var cacheBuster = Date.parse(new Date());

var flashvars = {};
var params = {};

params.bgcolor = '#F6F6F6';
params.allowfullscreen = 'true';


flashvars.stageW = stageW;
flashvars.stageH = stageH;

flashvars.pathToFiles = '';

flashvars.settingsPath = '../mp3player/mp3player_settings.xml';
flashvars.xmlPath = '<%# getRestXmlPlayerUrl() %>';

flashvars.keepSelected = 't';
flashvars.selectedWindow = '4';
flashvars.slideshow = 't';

flashvars.imageWidth = '130';
flashvars.imageHeight = '130';

swfobject.embedSWF('swf/preview.swf?t=' + cacheBuster, 'myContent', stageW, stageH, '9.0.124', 'swf/expressInstall.swf', flashvars, params);
});
</script>

一切都很好。但是,因为我在带有更新面板的页面上有一些 ajax,所以当 ajax 请求发生时,flash 没有呈现,所以我需要注册这个函数,我试过这样的事情:

protected void Page_PreRender(object sender, EventArgs e)
{
Type cstype = this.GetType();
String csnameForPlayer = "applyStyleToMp3Player";
if (!Page.ClientScript.IsClientScriptBlockRegistered(cstype, csnameForPlayer))
{
StringBuilder cstextForPlayer = new StringBuilder();
cstextForPlayer.Append(" $(document).ready(function() { "
+ " var stageW = 500;"
+ " var stageH = 216;"
+ " var cacheBuster = Date.parse(new Date());"

+ " var flashvars = {};"
+ " var params = {};"

+ " params.bgcolor = '#F6F6F6';"
+ " params.allowfullscreen = 'true';"


+ " flashvars.stageW = stageW;"
+ " flashvars.stageH = stageH;"

+ " flashvars.pathToFiles = '';"

+ " flashvars.settingsPath = '../mp3player/mp3player_settings.xml';"
+ " flashvars.xmlPath = '<%# getRestXmlPlayerUrl() %>';"

+ " flashvars.keepSelected = 't';"
+ " flashvars.selectedWindow = '4';"
+ " flashvars.slideshow = 't';"

+ " flashvars.imageWidth = '130';"
+ " flashvars.imageHeight = '130';"

+ " swfobject.embedSWF('swf/preview.swf?t=' + cacheBuster, 'myContent', stageW, stageH, '9.0.124', 'swf/expressInstall.swf', flashvars, params);"
+ "}); ");
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), csnameForPlayer, cstextForPlayer.ToString(), true);
}
}

好吧,这行不通。 Flash 播放器不再出现,因此,我认为 cstextForPlayer 中有问题。

我花了一个多小时来解决这个问题,但我失败了。

有人看到问题了吗?

提前致谢。

最佳答案

<%# getRestXmlPlayerUrl() %>是引起问题的部分。在 RegisterStartupScript 中使用时它不会返回任何值,但会被硬编码。现在,与其经历混合 C# 和 javascript 的所有痛苦,我建议您将此脚本外部化为一个函数,最好为正确的事情使用正确的工具:

function embedPlayer(xmlPath) {
var stageW = 500;
var stageH = 216;
var cacheBuster = Date.parse(new Date());

var flashvars = {};
var params = {};

params.bgcolor = '#F6F6F6';
params.allowfullscreen = 'true';


flashvars.stageW = stageW;
flashvars.stageH = stageH;

flashvars.pathToFiles = '';

flashvars.settingsPath = '../mp3player/mp3player_settings.xml';
flashvars.xmlPath = xmlPath;

flashvars.keepSelected = 't';
flashvars.selectedWindow = '4';
flashvars.slideshow = 't';

flashvars.imageWidth = '130';
flashvars.imageHeight = '130';

swfobject.embedSWF('swf/preview.swf?t=' + cacheBuster, 'myContent', stageW, stageH, '9.0.124', 'swf/expressInstall.swf', flashvars, params);
}

这将简化您的服务器端代码,只需调用前面的函数并将 xml 路径作为参数传递给它:

string csnameForPlayer = "applyStyleToMp3Player";
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), csnameForPlayer))
{
var script = string.Format("embedPlayer('{0}');", this.getRestXmlPlayerUrl());
ScriptManager.RegisterStartupScript(
this.Page,
this.Page.GetType(),
csnameForPlayer,
script,
true
);
}

关于asp.net - js在页面内部工作但不在代码后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2659082/

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