Plargato Plargato - 1 year ago 62
jQuery Question

calling java-script function after the post back to start a flash game

in my project i'd like to add a flash game, im using

<object width = "700" height = "500">
<param id = "game_param" value = "<swf_src>" name ="movie"/>
<embed id = "game_embed" src = "<swf_src>" width="700" height= "500">

to add the game to the page, and im using this jquery function to replace the attrabutes

function startGame(path) {
$("#game_param").attr("value", path)
$("#game_embed").attr("src", path)

my problem is, when i call this function with postback, the attrabutes are being reseted to thier orginal value, however when im calling it without postback while the attrabutes values are what i wanted the game isn't working. i tested and when i just entered the swf src to the source code it worked.

this is the button im using to call the jquery function:

<asp:Button ID="Button1" runat="server" OnClientClick="startGame("<real src>"); return false;" Text="Button" />

is there a way to do what im tring to do without creating a webpage for every game?

Answer Source

You can render anything on page after the post back, and in your case you can render the function call, here is how (base on your code):

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

    function startGame(path) {
        $("#game_param").attr("value", path)
        $("#game_embed").attr("src", path)
<asp:Literal runat="server" ID="txtJsOnMe" EnableViewState="false"></asp:Literal>

and on code behind

protected void Button1_Click(object sender, EventArgs e)
    txtJsOnMe.Text = "<script>startGame(\"<real src>\");</script>";

so after the post back will render the call right after the function, and you run your game.