Can't get javascript file to work...

M

Matthew Wells

Hello.

I thought this was supposed to be simple. I'm trying to use a .js file for
my javascript functions. I'm testing by only using one function The
function works when it's at the top of my aspx file.

The file is called "MyFunctions.js" which was imported on the page load
event of the code-behind form with
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyFuncs",
"MyFunctions.js", true); - I've used this with both true and false and with
adding script tags myself in the .js file.

I've also tried
Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "MyFuncs",
"MyFunctions.js");

The .js file is in the same directory as the page. and has only one
function:

function SayHello()
{
alert("Hello")
}

I added the function call on the code behind page load

btnLast.Attributes.Add("onclick", "SayHello()");

is there supposed to be some directive in the .js file?

Any ideas? This is driving me nuts!!!

Thanks.

Matthew Wells
(e-mail address removed)
 
M

Mark Rae [MVP]

Any ideas? This is driving me nuts!!!

First things first...

1) Does it work if you reference the file directly? E.g.

<head>
<script type="text/javascript" src="MyFunctions.js"></script>
</head>

2) JavaScript, generally speaking, requires semi-colons at the end of each
statement e.g.

function SayHello()
{
alert("Hello");
}

and

btnLast.Attributes.Add("onclick", "SayHello();");

Does that help...?

3) Have you inadvertently turned JavaScript off in your browser...?
 
M

Matthew Wells

I did have semicolons - I didn't type it right here. I know the
..attributes.add and the function itself work because it works when the
function is in the aspx page. It just doen't work when I put it in the
file. I also did try to reference it direclty.

Is there something needed at the top of the .js file?
Do I use RegisterClientScriptInclude or RegisterClientScriptBlock?

Also, and I know this probably doesn't matter, but... I'm using Visual
Studio 2005. I added the page using Add New Item - JScript File (There is
no "JavaScript" file choice.) This shouldn't make a difference because it's
just a text file with a .js extension, right? (he asked hopefully).

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top