How to run javascript function inside a ASPX file from a HTML file?

10.7k views Asked by At

I have a ASPX file to process some validation from my users. I need to prepear some codes and IDs for user to work with my data. I wrote a Validation.aspx file, checking everything about my users in Page_Load. I want to use some javascript functions from html files.

There are some javascript functions inside ASPX file, I make them in runtime from gathering data by validation.aspx Page_Load.

I want to put a script inside html files like this:

<script src="validation.aspx?a=1234" type="text/javascript" language="javascript" ><script/>

<script> RunValidationAnswer(); <script/>

The RunValidationAnswer(); function is made in runtime form user data (retriving from QueryString [a=1234] ). I can't access RunValidationAnswer(); in html files.

If i put the RunValidationAnswer(); in a JS file I can access it but I lose the powerfull operations inside Page_Load of aspx file.

plz help me to find a way to solve my problem.

I wrote this sample script in Validation.aspx

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

It is compeletely accessible inside validation.aspx but I can't access this function from other files.

I want somthing like this inside other files:

<script src="validation.aspx?a=1234" type="text/javascript" language="javascript" ><script/>

<script> RunValidationAnswer(); <script/>
1

There are 1 answers

6
Michael B. On

Did you set the content type of ASPX page to be javascript so the browser would know that is Javascript file, cause by default the content type of any aspx is HTML

Response.ContentType = "text/javascript"

** add ; for C#

Validation.aspx should be empty file except for this line

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ScriptTest.aspx.cs" Inherits="ScriptTest" %>

And in code file, you write JS by response.write

public partial class ScriptTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/javascript";

        Response.Write("alert('javascript works')");
    }
}

You could also just use ASPX page -without code file- like this

<%@ Page Language="C#" AutoEventWireup="true" ContentType="text/javascript" %>
alert("script works <%=DateTime.Now.ToString() %>");