Pinu
Pinu

Reputation: 7520

Using Javascript function on other page

I have a javascript function in the default.aspx page of my asp.net website project. I need to use that page in my myfolder/test1.aspx page. How can i refer to other page? without having to create a js file of my javascript page on default.aspx page...

Upvotes: 0

Views: 137

Answers (4)

joelt
joelt

Reputation: 2680

A separate .js file is probably the best way to do it. If there are dynamic values being generated by the default.aspx page, you could make your .js file something like CommonJS.aspx, and have that return JavaScript instead of markup. Also, you could create a user control that contains the JS code, and include the user control on multiple pages.

Upvotes: 0

Dani Cricco
Dani Cricco

Reputation: 8979

You can't refer to embedded JS files in other pages. You should create a separate .js file and include it in the pages where you want to use it.

Upvotes: 1

Rahul
Rahul

Reputation: 1876

It is not possible to do so without having a common js file containing the functions.

Upvotes: 1

ChrisF
ChrisF

Reputation: 137188

The Javascript has to be loaded for each page. So unless you put it in a separate .js file which you reference from each page you'll have to repeat the code - which is a very bad idea.

Just go with the separate .js file. You probably already have .css files you reference - it's no different to that.

Upvotes: 2

Related Questions