Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

Can't get javascript file to work...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Matthew Wells - 01 Mar 2008 18:45 GMT
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
Matthew.Wells@FirstByte.net
Mark Rae [MVP] - 01 Mar 2008 19:08 GMT
> 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...?

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Matthew Wells - 01 Mar 2008 19:29 GMT
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.

>> Any ideas?  This is driving me nuts!!!
>
[quoted text clipped - 21 lines]
>
> 3) Have you inadvertently turned JavaScript off in your browser...?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.