0

Using C# .NET 3.5 Framework here to make an .aspx web page. Anyways, the page accepts a GET variable x, and if supplied, I want the page to hook a JavaScript function foo(x) to the onload event of the page. The GET variable must be passed as an argument to foo.

I'm more familiar with other server-side scripting languages, and for example, in php I might do something like the following snippet, where I embed the php right into the <head> of the html page in the <script> (for simplicity I just set foo to the window.onload as I probably will only ever have one function set to this event anyways):

...

<script language="javascript" type="text/javascript">
function foo(x) {
    alert( x );
}

<?php

if( isset($_GET["x"]) )
    echo "window.onload = foo('" . $_GET["x"] . "');";
?>
</script>

...

Alternatively, I make the .js file a php parsed file that outputs the same and link that from the html file via src attribute of <script> (but I would still need a bit of embedded PHP to pass the GET variable to that script file src URL).

Anyways, with this aspx stuff, it looks a lot different, in that I have an underlying .cs file and there seems to be a huge strive to separate logic like this from the content of the page (.aspx) file. I wasn't really sure how to get this same kind of behavior in this kind of environment.

It's kind of not really ideal to have my JavaScript source inside my C# source as a string, etc.

Anyways, I did some searches on this but I can't really find a clean solution. Anyone have a sensible solution for this type of thing in .aspx?

I should also point out that I do not want to rely on JavaScript to parse the window.location for the GET variables, as I use extensive path rewriting and I want to be flexible for POST variables.

UPDATE

Ok, so using the answers so far that are more focused on asp side of things I can mimic the php flavor as close as possible with something like this:

...
    <script language="javascript" type="text/javascript">

    function foo(x) {
        alert( x );
    }


    <%    
       String x = this.Request["x"];
       if(!String.IsNullOrEmpty(x))
           Response.Write( "window.onload = foo('" + Server.UrlDecode(x) + "');" );

    %>
</script>
...

Though this works, it's not exactly the answer I am looking for. I was hoping for an aspx.cs solution that would divorce the need to embed this kind of code in my html file. Perhaps I was misleading when I gave the PHP example. I'd like to handle this on the .cs side of the world rather than embedding it directly in the html of the aspx file. Any ideas?

2 Answers 2

1

I would define some global variables in your own script block and leave the main JS file alone.

<script type="text/javascript">
var foo = <% = yourVar %>
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

in aspx (not in aspx.cs):

<% String x = this.Request["x"]; if (!String.IsNullOrEmpty(x)) { %> <script type="text/javascript"> window.onload = foo('<%=x%>'); </script> <% } %>

1 Comment

Why cannot I add comment to the original post? "I was hoping for an aspx.cs solution that would divorce the need to embed this kind of code in my html file." I am confused by "embed this kind of code in my html file". Do you mean you don't want to write code like "window.onload = ..." to the response?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.