views:

135

answers:

4

hi,

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>

    <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>
+1  A: 

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

Chuk Lee
thank you for response Mr.Chuk Lee just now i place the comment can u check the comment please
Surya sasidhar
Ok your question above is not very clear either. I'm going to interpret it as you want to dynamically generate JS inside a web page and then return that web page to your user. One simple way, if you are doing it in Java, is to create a servlet mapping to /myapp/dymamicpage. Then what you do is use StringTemplate to generate the JS code, set the content type to 'text/html' and return it to your user.
Chuk Lee
Miss that part about querystring. So if you have something like this /myapp/dymamicpage?movefile=movie.avi then use HttpServletRequest.getParameter("moviefile") to retrieve the parameter(s)
Chuk Lee
i am using .net not java Mr.Chuk Lee
Surya sasidhar
+3  A: 

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>
iolo
A: 

Let's say that you have this URL:

http://www.myplayer.com&amp;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.

kgiannakakis
+1  A: 

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.

Joel Mueller
ya Mr.Joel Mueller it is working fine thank you
Surya sasidhar