开发者

how can i write dynamic javascript

how can i write dynamic javascript any simple example or any references. thanking you

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

 开发者_StackOverflow   <script type="text/javascript" src="swfobject.js"></script>

<div id="flashPlayer">

</div>

<script type="text/javascript">




   var so = new SWFObject("playerSingle.swf", "mymovie", "192", "67", "7", "#FFFFFF");
   so.addVariable("autoPlay", "yes");
   so.addVariable("soundPath","song.mp3");
   so.addVariable("overColor","#000044")
   so.addVariable("playerSkin","1") 
   so.write("flashPlayer");


</script>
    </div>

    </form>
</body>
</html>


This is a very broad question.

If you mean how can you use javascript to set the audio file parameter of the swf object, then if we take the code you already have and assume your URL to be something like http://mydomain.com/musicplayer.html?audio=mysong.mp3, there is a library at http://adamv.com/dev/javascript/querystring that can help. Using it should result in something similar to this:

<script type="text/javascript">

   //assign variable to querystring
   var qs = new Querystring()

   //retrieve the parameter you want
   var requestedFile = qs.get("audio", "defaultsong.mp3")

   var so = new SWFObject("playerSingle.swf", "mymovie", "192", "67", "7", "#FFFFFF");
   so.addVariable("autoPlay", "yes");
   so.addVariable("soundPath",requestedFile);
   so.addVariable("overColor","#000044")
   so.addVariable("playerSkin","1") 
   so.write("flashPlayer");


</script>


If you would like to use ASP.NET to parse the query string rather than javascript, something like this ought to work:

<script type="text/javascript">

   var so = new SWFObject("playerSingle.swf", "mymovie", "192", "67", "7", "#FFFFFF");
   so.addVariable("autoPlay", "yes");
   so.addVariable("soundPath", '<%= Request.QueryString["song"] %>');
   so.addVariable("overColor","#000044")
   so.addVariable("playerSkin","1") 
   so.write("flashPlayer");

</script>

However, if you do this, you will have a security hole - a custom-crafted URL could embed anything into the javascript executing on your page, including malicious code. You should use the AntiXSS library to javascript-encode the value from the query string before you insert it into your client-side javascript.


I'm going to interpret your question as dynamically generating Javascript and then executing it. If you are using Java 6, then you can do something like this

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine js = mgr.getEngineByExtension("js");
try {
   js.eval("print('Hello, world!')");
} catch (Exception e) { }

If you need to generate very complex JavaScript code, the I suggest you use a template library like StringTemplate


Let's say that you have this URL:

http://www.myplayer.com&song=mysong.mp3

Being redirected to this page, you want to play the mysong.mp3.

In javascript you can read the url using the window.location.href property. You need to parse it yourself in order to extract the song.

This link could help you with the parsing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜