views:

1208

answers:

4

How can I send parameters (with QueryString) within a javascript tag as the source attribute to a ASP.NET page?

Example: <script language="javascript" src="myDomain/myPage.aspx?id=123&no=43"></script>

And what I have to do in "myPage.aspx"?

For example I want to send a picture to the script tag according to it's src querystring.

+3  A: 

The script tag is used to include javascript code in the page. If you want to show an image on the page, even one generated dynamically, you want to use an img tag, not a script tag.

<img src="myDomain/myPage.aspx?id=123&no=43" alt="some text" />

Normally, you'd use an HttpHandler for this (ashx instead of aspx) and it would simply need to construct the image (or read it from a file) and then send the data down in the response with the correct MIME-type, length, etc.

See this reference on how to retrieve images from a DB using an HttpHandler.

tvanfosson
+1  A: 

Why would you send a picture to a script tag? Basically what you have will work client side. In MYPage.aspx you need to output what you want to send.

I'd recommend using an HttpHandler which is a great to dynamically provide things like CSS, Javascript or images

JoshBerke
And many more: http://stackoverflow.com/questions/386142/ihttphandler-example-required-for-image-type-files-c-asp-net/386171
Zhaph - Ben Duguid
There are tons I just picked the ones I answered;-) easier to pull them out
JoshBerke
+2  A: 

It's not clear what you intend to do in your myPage.aspx. Since it is a script tag, it should be generating javascript code. But I don't see any reason why you'd need to dynamically generate your javascript code. Javascript variables basically have global scope, so define the image in a variable before including the script tag.

So in your html page you would do something like this in your header:

<script type="text/javascript">
var imageURL = 'http://www.google.com/intl/en_ALL/images/logo.gif';
</script>
<script src="myScript.js" type="text/javascript"></script>

And then in myScript.js:

alert("The image URL is: " + imageURL);
//do whatever processing with the image that you need to do...

Google Analytics used to work like this (before they went to a more object-oriented approach).

Kip
A: 

All you have to do is give your tag a SRC attribute pointing to an ASPX page, just like you wanted. The only trick is that you have to have the ASPX page that returns javascript set the contentType to "text/javascript". (Make sure it sends back only valid javascript.)

Here are two files to prove that it works:

-- JavascriptLibraryTester.aspx --
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script src="JavascriptLibraryTest.js.aspx?Color=red" type="text/javascript" charset="utf-8">
</script>
</head>
<body>
<a href="javascript:showServerGeneratedJavascript()">show Server Generated Javascript</a>
</body>
-- /JavascriptLibraryTester.aspx -----------------------------------------------------


-- JavascriptLibraryTest.js.aspx ------------------------------------------------------
<%@ Page Language="C#" %>
<%
Response.ContentType = "text/javascript";
string color = Request["Color"];
string now = DateTime.Now.ToString();
%>
function showServerGeneratedJavascript(){
alert('<%=now %>\n<%=color %>');
}
-- /JavascriptLibraryTest.js.aspx -----------------------------------------------------

joe_tansy